eof函数如何在cpp上工作
how eof function work on cpp?
我知道函数"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;
}
- 有没有办法让这个模板在 cpp 中工作?
- 使用.cpp文件让 RcppArmadillo 在 Rstudio 中工作
- 创建链表,但无法按 CPP 的预期工作
- 仅一个功能的链接错误.在同一类和CPP文件中具有相似签名的其他函数工作正常
- 运算符重载在 CPP 中的排序中如何工作?
- 简单的 CPP 地图存储和访问无法按预期工作
- (c++)无法让 void 函数工作,主.cpp内部的几行也有问题
- CPP / JUCE 音频编程框架 - 滑块数组不起作用 - 单个滑块工作
- 程序编译和运行,但一段时间后,它停止使用Typedef关键字在CPP上工作
- CPP:超载的嵌套操作员无法正常工作
- "string::erase"如何在 cpp 中工作?
- 在macOSX上编译boost_asio/example/ssl/server.cpp可以在Linux上工作
- 不完整的参数错误,包括其他.cpp文件中的postgres.cpp工作文件
- 从工作iOS项目中导入.cpp文件并将其重命名为.mm问题
- CPP MQTT Broker代码不工作
- eof函数如何在cpp上工作
- 为什么在多个 cpp 文件中包含相同的标头,然后它们的编译工作
- CPP GDB 崩溃,没有核心和 GDB 附加到工作进程
- 相对于 cpp 不工作目录的路径
- 不确定查找功能在 CPP 中的工作原理