Istringstream - how to do it?

I have a file:

a 0 0
b 1 1
c 3 4
d 5 6

Using istringstream, I need to get a, then b, then c, etc. But I do not know how to do this, because there are no good examples on the Internet or in my book.

Code so far:

ifstream file;
file.open("file.txt");
string line;

getline(file,line);
istringstream iss(line);
iss >> id;

getline(file,line);
iss >> id;

Prints "a" for id at the same time. I do not know how to use istringstream, and I have to use istringstream. Please, help!

+3
source share
2 answers
ifstream file;
file.open("file.txt");
string line;

getline(file,line);
istringstream iss(line);
iss >> id;

getline(file,line);
istringstream iss2(line);
iss2 >> id;

getline(file,line);
iss.str(line);
iss >> id;

istringstreamcopies the line you give it. He cannot see the change line. Either create a new line stream, or make it take a new copy of the line.

+5
source

You can also do this by having two while loops: - /.

while ( getline(file, line))
{
    istringstream iss(line);

    while(iss >> term)
    {
        cout << term<< endl; // typing all the terms
    }
}
+2

Source: https://habr.com/ru/post/1734197/


All Articles