The C + + EOF () function helps us to determine whether the file is empty or whether it reads the end of the file. We will introduce it in detail here.
Many of the features in the C + + programming language play a big role in our practical application. For example, in the operation of the file text, it can be implemented in a variety of ways. Here we introduce the C + + EOF () function is one of the more common basic functions.
The C/C + + read the file, must have been used to determine whether the file is empty or read to the end of the file, but also in the process of using this function encountered some problems, such as can not accurately determine whether it is empty or whether to the end of the file, Some people may wonder if the function itself is problematic in design.
Let's take a look at the following code:
#include < iostream> #include < fstream> using namespace std; int main () { char ch = ' x '; Ifstream fin ("test.txt"/*, ios::binary*/); if (fin.eof ()) { cout < < "file is empty." < < Endl; return 0; } while (!fin.eof ()) { fin.get (CH); cout < < ch; } System ("pause"); return 0;
Compile and run the above code,
- C + + operator overloading different ways to differentiate
- A detailed introduction to C + + file stream application methods
- Interpretation of C + + declarative Syntax method
- Basic concept Analysis of C + + KDevelop
- Summary of basic contents of C + + virtual destructor function
If Test.txt does not exist, the program will form a dead loop, fin.eof () will always return false, if the test.txt is empty, the program prints an x character, when there is a string "ABCD" in Test.txt and there is no newline, the program prints "ABCDD", When the above string is present and there is a new empty line, the program prints "ABCD" plus a blank row.
This phenomenon may confuse many people, the results of the program seems to be very unstable, when the wrong time. The same results are used when reading in binary mode. Here, you may have a misconception that EOF () returns True when the last character is read to the file, in fact, the C + + EOF () function returns True when it is read to the file Terminator 0xFF, and the file terminator is the next character of the last character.
Therefore, when the last character is read, the program reads it one more time (the compiler keeps the pointer over the last character and then reads it again, which is why the last character above is output two times. As for whether all the compilers are doing this I'm not quite sure, I use the vc6,vc8 seem to be like this)
Problem comes out, it is necessary to find the corresponding solution, to solve the above problems, only need to adjust the conditional statement can:
Fin.peek () = = EOF or fin. Get
Let's look at another scenario:
#include < iostream> #include < fstream> #include < string> using namespace std; int main () { string str; Ifstream fin ("test.txt"/*, ios::binary*/); if (fin.peek () = = EOF) { cout < < "file is empty." < < Endl; return 0; } while (!fin.eof ()) { fin >> str; cout < < str; } System ("pause"); return 0;
The above code is compiled and run under VC8, it is found that when there is no empty line at the end of the file, the result is correct, when the end of the line is empty, the last string will be repeated output once, while the VC6 situation is different, there is no duplicate output, but output a blank row.
Therefore, in order to ensure that we expect the results to be consistent under different compilers, the conditional statements are modified:
In summing up the description of the C + + EOF () function, we can get the following conclusions:
1. The Peek function is used to determine if the file is empty, and if Peek returns EOF then the file is empty;
2. Read the file process, read non-char type, use peek to determine the end of the file will no longer apply, the loop judgment condition should use the >> operator to read, if read into the char buffer, peek function will behave very well.
C + + EOF () function