Sign in to follow this  

ifstream--reading with stream operators and also getline()?

This topic is 4341 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

I'm giving myself a headache here--I want to read a file formatted like this: 1.0 1.43 0 1.2 2.01 0.41 0.31 0.44 0.32 c:\some folder\some file.dat So I have an ifstream, and I just use the shifty operators to read the numbers:
for(int j = 0; j < a lot; j++)
    f >> m_fData[j];
But when I get to the end and have to read the whole line, I just can't think of a pretty way to do it. I can't, of course, just use f >> somestring, because I'd just get "c:\some" in the string. I could use getline(), but the >> operator doesn't finish off the newline from the last line of floats, so I get a blank line. I could use two getlines, but that's just plain ugly. I could do getline()'s all around, but that would turn the float-processing code fugly. Any ideas?

Share this post


Link to post
Share on other sites
Quote:
Original post by BeanDog
I could do getline()'s all around, but that would turn the float-processing code fugly.


Maybe not as fugly as you think. You probably don't need it here, but for complicated cases, you could read your file a line at a time (via std::getline) into std::strings, wrap std::stringstreams around them, and then re-parse individual lines - the stringstream provides a stream interface to a string buffer, "shifty operators" (lol) and all.

Share this post


Link to post
Share on other sites

This topic is 4341 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this