eof函数如何在cpp上工作

how eof function work on cpp?

本文关键字:cpp 工作 函数 eof      更新时间:2023-10-16

我知道函数"eof"(cpp)只有在错误地尝试从文件读取后(而不是在我到达文件末尾时)才返回"True"

因此,如果我们想将所有文件从1移动到另一个,我们必须进行

infile.get(c);
while ( !infile.eof() )
{
   outfile << c;
   infile.get(c);
}

而不是

while ( !infile.eof() )
{
   infile.get(c);
   outfile <<c;
}

因为如果我们做第二种方式,最后一个字符将复制2次

但在另一个程序上,它不像那样工作

我创建文件grades.txt并在上面写"dani"

代码如下:

ifstream inGrade("grades.txt");
ofstream outRoster("roster.txt");
int tmpGrade;
inGrade >> tmpGrade;
while (!inGrade.eof() ) 
{
    outRoster << tmpGrade <<endl ;
    inGrade >> tmpGrade;
}

它创建了"花名册.txt",但没有复制任何内容。

但如果我使用这个代码:

ifstream inGrade("grades.txt");
ofstream outRoster("roster.txt");
int tmpGrade;

while (!inGrade.eof() ) 
{
    inGrade >> tmpGrade;
    outRoster << tmpGrade <<endl ;
}

它将创建roster.txt并将"dani"复制到

为什么???为什么在这个例子中,当我们到达文件的末尾时,eof返回false,而不是在错误地尝试从文件中读取之后。

我创建文件grades.txt并写在这个"dani"上

所有的读取操作都应该失败,因为"dani"不能被提取为整数。这设置了流的失败位,但不使用任何字符,因此不设置eofbit。你的两个程序都应该被困在一个无限循环中。

修复我不放dani我放"100"

好吧,那么你就不会得到无限循环了:)我已经写了一个程序来演示这个问题:

istringstream input("100");
int foo;
cout << "Reading int succesfully" << endl;
input >> foo;
cout << "!input:t" << boolalpha << !input << endl;
cout << "input.eof():t" << boolalpha << input.eof() << " << pay attention" << endl << endl;
cout << "Attempting to read eof" << endl;
input >> foo;
cout << "!input:t" << boolalpha << !input << endl;
cout << "input.eof():t" << boolalpha << input.eof() << endl << endl;
input.clear();
input.str("c");
char c;
cout << "Reading char succesfully" << endl;
input >> c;
cout << "!input:t" << boolalpha << !input << endl;
cout << "input.eof():t" << boolalpha << input.eof() << " << pay attention"  << endl << endl;
cout << "Attempting to read eof" << endl;
input >> c;
cout << "!input:t" << boolalpha << !input << endl;
cout << "input.eof():t" << boolalpha << input.eof() << endl << endl;

输出:

Reading int succesfully
!input:      false
input.eof(): true << pay attention
Attempting to read eof
!input:      true
input.eof(): true
Reading char succesfully
!input:      false
input.eof(): false << pay attention
Attempting to read eof
!input:      true
input.eof(): true

因此,与读取数字等格式化输入相比,eofbit在读取单个字符时的行为有所不同。

因此,如果您想修改循环的版本,使其对数字和字符的行为方式相同,则需要使用bool转换而不是eof()来检查流状态。此外,这将防止对无效输入进行无限循环。您可以使用fail(),但这不会检查坏比特,因此当您出现i/o错误时,它不会有所需的行为。

infile.get(c);
while (infile) // or !infile.fail() if you have infallible hardware
{
   // use c
   infile.get(c);
}

应该和一样工作

int tmpGrade;
inGrade >> tmpGrade;
while (inGrade) 
{
    // use tmpGrade
    inGrade >> tmpGrade;
}

但是,您的方法会重复输入调用。你可以通过在循环中输入条件来避免这种情况:

while (inGrade >> tmpGrade)
{
    // use tmpGrade
}

如果在读取值时达到文件末尾,格式化输入将触发文件末尾条件。这意味着,如果输入文件中的最终值之后没有任何内容,那么"校正"的循环将无法输出最终值。特别是,如果输入文件中只有一个值,就不会有任何输出。

您需要不断尝试读取,直到读取失败,而不是检查文件的末尾。最后一个值不会失败(但可能会设置eof);之后的下一次尝试将失败,这表明您应该停止。

作为一个额外的好处,这样做的代码比你的背靠背循环更少令人困惑:

while (inGrade >> tmpGrade) {
    outRoster << tmpGrade << endl;
}