下面的代码是为了从文件“float.txt”中计算所有浮动数字。问题是浮动数字之间有垃圾信息。为什么浮动输入文件不工作C++
示例文件:
23.5 aujsaN8.2<:::32
第一浮点后得到,while循环永远不会结束,程序不再从文件的任何信息。请帮忙。
int main()
{
float num;
ifstream input("float.txt");
input >> num;
while (!(input.eof()))
{
input >> num;
}
input.close();
return 0;
}
你想要发生什么? – 0x499602D2 2014-11-07 00:17:43
明显的解决方案是在处理之前清理float.txt。 – 2014-11-07 00:41:21
[不要在循环条件中使用eof](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – 2014-11-07 00:41:42