C + + Read the file last line read twice

Source: Internet
Author: User

With the Ifstream EOF (), even read the file at the end, to determine the EOF is false. After finding the information online, we finally solved the problem.

Reference file: http://tuhao.blogbus.com/logs/21306687.html

When you read a file by using C + +, you must use the EOF () function to determine if the file is empty or read the end of the file, and you will encounter some problems in the process of using this function, such as not being able to determine whether it is empty or whether it is at 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,

If Test.txt does not exist, the program will form a dead loop, fin.eof () will always return false,
If 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 out "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, but not, EOF () returns True when the file terminator is read to 0xFF, and the file terminator is the next character of the last character. As shown in the following:


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 (CH)

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:
Fin >> Str

To sum up, 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.

Peek () --This function returns the next character of the input stream file, but it does not move the built-in pointer.

C + + Read the file last line read twice

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.