istringstream

I need to extract all the sequences of non-empty characters (i.e. words) from all lines in a ascii inp_file. The following code does not go beyond the very first non-empty string, i.e. item is repeatedly the very first word of the first line.

while (getline(inp_file, line)) {
while (istringstream(line) >> item) {
out_file << item << '\n';
}
}

I thought that istringstream applies the stream attributes to its argument, making it behaving like a stream.

What am I doing wrong? How can I extract all the words in inp_file?
You don't need the stringstream object to do that. Try:
1
2
3
4
while( inp_file >> item )
{
    //...
}


If you did want to get a string (line) at a time from the file and process it via a istringstream, it goes like this:
1
2
istringstream ss( line );  // create a stringstream from line
ss >> item;  // read a word from the stringstream 


Note that in the previous examples, inp_file was a previously declared ifstream and item was a previously declared string.
Seymore, Thanks a bunch! It works and I really appreciate it.

Now I must understand the synatx you used and also why mine was incorrect.
Visit the Reference section of this site. It has everything that you need to know about stream constructors and their operators.
Topic archived. No new replies allowed.