Sign in to follow this  

feof in standard c++ library

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

Hello! I'm switching from the old c library (printf) to the new c++ one (cout) and I need to know if there's a function similar to feof() to be able to do:
while(! end_of_file(my_file) )
{
  go_on_and_read_for_everlasting_peace(my_file);
}
What is this function? or if it doesn't exist, what's the workaround?

Share this post


Link to post
Share on other sites
If you are using the iostream libraries, and not the stdio.h (and you realy should in C++)
you can use the ifstream::eof() function.

ifstream fin;
...
while(!fin.eof())
{
...
}

or

while(fin.good())
{
...
}


Im not sure if they do the exact same thing though...

EDIT:
Whow, I must be typing slow...

Share this post


Link to post
Share on other sites
file.good() doesn't just refer to end of file. file.eof() does.

file.good() can become false on EOF, on a failed input operation (e.g. you try to read a number and there are only letters there) or if the stream itself goes "bad".

file.good() == !( file.eof() | file.fail() | file.bad() );

Share this post


Link to post
Share on other sites

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