Jump to content
  • Advertisement
Sign in to follow this  
BeanDog

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

This topic is 4558 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!