# 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.

## 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 on other sites
Use istream::ignore() to remove characters from a stream.

##### Share on other sites
Just what I was looking for, thanks! Somehow I've missed that one all these years.

##### Share on other sites
Quote:
 Original post by BeanDogI 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.

1. 1
2. 2
3. 3
Rutin
16
4. 4
5. 5

• 10
• 14
• 30
• 13
• 11
• ### Forum Statistics

• Total Topics
631788
• Total Posts
3002357
×