-
getline() help
I am having problems with a program I am trying to write. I am using the getline() method to read lines of a file. It functions correctly until a blank line is encountered. Then it stops executing. Is there a way I can catch an exception or something to skip the blank line and continue on the other side?
Here is the section of my code causing the problems. I'm positive it's the getline() statement that's causing the problem. It can't get an empty line.
Code:
while(end != true)
{
getline(sceneFile, fileBuffer);
tokens = tokenizer(fileBuffer, tokens);
I'd appreciate some help. It's been a long time since I've used c++. Been more of a java junkie for some time.
Thanks,
Stephen
-
This is what I generally do -
Code:
while ( getline( MyFileStream, MyStringVariable ) )
{
// do stuff
}
That will keep on reading till it gets to the end of the file. If it encounters a blank line, just check -
if ( MyStringVariable == "" ) // do something about it
-
Thanks for the quick reply, twomers.
I'm not sure that will fix my problem though.
The difficulty I'm having is that getline(fs, sv) is throwing some sort of exception that is causing execution to end immediatly.
I'll try that though and see what happens.
-
cout<< sv;
and see what happens, in the while loop.
Check to see if SV=="" as well
EDIT - [COLOR="#f1f1f1"]Olé, 1000 posts!![/COLOR]
-
Thanks man,
I actually just got this to work by putting an
Code:
if(fileBuffer.empty()) //Checks for empty line
continue;
-
>> while ( getline( MyFileStream, MyStringVariable ) )
This checks for errors, but getline succeeds even if the line is empty. It is still a good idea to do it this way anyway, and then add the check for the empty string.
-
>> This checks for errors, but getline succeeds even if the line is empty. It is still a good idea to do it this way anyway, and then add the check for the empty string.
Or put into code:
Code:
while ( getline( F_S, S_V ) )
{
if ( S_V == "" ) // or the empty() thing. EDIT - Whoops, I had a == 0 first
{
// Do stuff
}
}
should do it
-
Thanks guys.
My code is working well now.