Jump to content
  • Advertisement
Sign in to follow this  

feof in standard c++ library

This topic is 4499 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
Advertisement
if you use file streams, the good() method:


std::ifstream in("in.txt");
while( in.good() )
{
// do stuff
}

there is also a eof() method

Share this post


Link to post
Share on other sites
#include <fstream>
#include <string>

std::ifstream file( "filename" ); //std::ifstream is-a std::istream

while ( file.good() ) //includes "!file.eof()"
{
std::string line, word;
char character;

std::getline( file , line );
file >> word;
file >> character;
}


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
Quote:
Original post by pulpfist
Im not sure if they (!eof and good) do the exact same thing though...


They don't.

file.good() is synonymous with !( file.eof() || file.bad() || file.fail() )

EDIT: Beaten by Fruny D:

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!