Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does ifstream's eof() work?

Tags:

c++

eof

ifstream

#include <iostream> #include <fstream>  int main() {     std::fstream inf( "ex.txt", std::ios::in );     while( !inf.eof() ) {         std::cout << inf.get() << "\n";     }     inf.close();     inf.clear();     inf.open( "ex.txt", std::ios::in );     char c;     while( inf >> c ) {         std::cout << c << "\n";     }     return 0; } 

I'm really confused about eof() function. Suppose that my ex.txt's content was:

abc 

It always reads an extra character and shows -1 when reading using eof(). But the inf >> c gave the correct output which was 'abc'? Can anyone help me explain this?

like image 903
Chan Avatar asked Dec 26 '10 07:12

Chan


People also ask

What does Cin EOF do?

cin. eof() test if the stream has reached end of file which happens if you type something like Ctrl+C (on Windows), or if input has been redirected to a file etc. To test if the input contains an integer and nothing but an integer, you can get input first into a string and then convert that with a stringstream.

How do you use EOF?

Use EOF to avoid the error generated by attempting to get input past the end of a file. The EOF function returns False until the end of the file has been reached. With files opened for Random or Binary access, EOF returns False until the last executed Get statement is unable to read an entire record.

How does EOF work C++?

C++ provides a special function, eof( ), that returns nonzero (meaning TRUE) when there are no more data to be read from an input file stream, and zero (meaning FALSE) otherwise. Rules for using end-of-file (eof( )): 1. Always test for the end-of-file condition before processing data read from an input file stream.

How do I read the last line of a file in C++?

Use seekg to jump to the end of the file, then read back until you find the first newline.


1 Answers

-1 is get's way of saying you've reached the end of file. Compare it using the std::char_traits<char>::eof() (or std::istream::traits_type::eof()) - avoid -1, it's a magic number. (Although the other one is a bit verbose - you can always just call istream::eof)

The EOF flag is only set once a read tries to read past the end of the file. If I have a 3 byte file, and I only read 3 bytes, EOF is false, because I've not tried to read past the end of the file yet. While this seems confusing for files, which typically know their size, EOF is not known until a read is attempted on some devices, such as pipes and network sockets.

The second example works as inf >> foo will always return inf, with the side effect of attempt to read something and store it in foo. inf, in an if or while, will evaluate to true if the file is "good": no errors, no EOF. Thus, when a read fails, inf evaulates to false, and your loop properly aborts. However, take this common error:

while(!inf.eof())  // EOF is false here {     inf >> x;      // read fails, EOF becomes true, x is not set     // use x       // we use x, despite our read failing. } 

However, this:

while(inf >> x)  // Attempt read into x, return false if it fails {     // will only be entered if read succeeded. } 

Which is what we want.

like image 187
Thanatos Avatar answered Oct 10 '22 15:10

Thanatos