• Advertisement
Sign in to follow this  

end of a stingstream?

This topic is 4327 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im trying to write a small program which takes a series of numbers... "1 2 3 4 5" I want to then extract those numbers into an std::list... So far I have managed to do it, the numbers are all stored in a list and it works correctly. But my problem is that I have to terminate the string with -1 to tell that im at the end of the stringstream. Is there a way to tell that im reading the last number from the stream? Regards Ashley

Share this post


Link to post
Share on other sites
Advertisement
After an operation that reads multiple characters (such as integer formatted input), if the end of the stream has been reached, the eof bit is set. You can test it with the eof() member function.

After an operation that reads one character at a time (such as get()), the eof bit is set if you've tried to read past the end. The read operation returns garbage.

In essence, multi-character input functions discard the post-eof garbage, leaving you with clean data as well as setting the flag.

Quote:
I want to then extract those numbers into an std::list...


Sounds like a job for a stream iterator:

#include <list>
#include <string>
#include <sstream>
#include <iterator>

std::string input = "1 2 3 4 5";
std::istringstream iss(input);

std::istream_iterator<int> begin(iss);
std::istream_iterator<int> end;

std::list<int> l(begin, end); // or l.assign(begin,end) if the list already exists.

Share this post


Link to post
Share on other sites
Woops.

Yes it does I tested it,

Cheers fruny.

.. Asheh

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement