Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
139 views
in Technique[技术] by (71.8m points)

c++ - How to read everything inside a double quoted string?

I'm quite new to C++. I'm writing a program that reads data from a file and puts it into an object. I'm doing this to try to understand the iostream better.

Each line of the file contains an alphanumeric ID in the format nxn, where n is some integer, and x is some character, a color, another integer, and then a location. Here's an example

17832X9647 "blue" 100 "New York City"

Unfortunately, the code that I've written doesn't get all of the final string, since it has spaces in it. So, I tried writing a function that would return all of the text inside double quotes (not including the double quotes) and putting it inside the input stream, while still preserving the whitespaces and discarding the double quotes. Unfortunately, that also did not work, so now I'm sitting here scratching my head, not knowing what to do, and it's because I fundamentally don't understand how the iostream works. What can I do to fix this? I would really, really not like to restructure my whole program, because I want to learn exactly how I'm treating the iostream wrong. Getting this thing to work in the most efficient way isn't important to me, just that I understand more about the iostream.

Here is my code.

#include <iostream>
#include <string>
#include <fstream>


using namespace std;

string ReadDoubleString(istream & in)
{
    string output_string;

    char ch = '"';

    in.ignore(ch); //i'm thinking that this will make the extraction skip any double quotes
    in >> output_string;

    return output_string;
}


//begin class declaration and implementation for Widget
class Widget
{
    friend istream& operator>>(istream&, Widget&);
    
    public:
        string ID;
        string color;
        int Age;
        string location;

        void setAttributes(string, string, int, string);
};

void Widget::setAttributes(string ID, string color, int Age, string location)
{
    this->ID = ID;
    this->color = color;
    this->Age = Age;
    this->location = location;
}

istream& operator>>(istream& in, Widget& output_widget)
{
    int input_ID_pt1;
    char input_ID_pt2;
    int input_ID_pt3;
    string full_ID;

    string input_color;

    int input_Age;

    string input_location;


    in >> input_ID_pt1 >> input_ID_pt2 >> input_ID_pt3;
    input_color = ReadDoubleString(in);
    in >> input_Age;
    input_location = ReadDoubleString(in);  

    full_ID = to_string(input_ID_pt1) + input_ID_pt2 + to_string(input_ID_pt3);

    output_widget.setAttributes(full_ID, input_color, input_Age, input_location);

    return in;
}
//end class declaration and implementation for Widget


int main()
{
    string file_name;

    cout << "enter file name" << endl;
    cin >> file_name;

    ifstream newFile(file_name);
    
    if (newFile.is_open())
    {
        Widget w1;

        newFile >> w1; 

        cout << w1.ID << endl;
        cout << w1.color << endl;
        cout << w1.Age << endl;
        cout << w1.location << endl;
    }
    else
    {
        cout << "could not open file!" << endl;
    }

    return 0;
}

My program ends up returning this when I use the data from earlier.

17832X9647
//empty line
0
//empty line
//empty line

Thank you so much for reading.

question from:https://stackoverflow.com/questions/66061433/how-to-read-everything-inside-a-double-quoted-string

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...