我有一个非常简单的代码,但我无法找出错误。任务:我想读取包含浮点/双精度值的文本文件。文本文件如下所示:
--datalog.txt--
3.000315
3.000944
3.001572
3.002199
3.002829
3.003457
3.004085
3.004714
3.005342
3.005970
3.006599
3.007227
3.007855
3.008483
3.009112
3.009740
3.010368
3.010997
代码看起来像这样
--dummy_c++.cpp--
#include <iostream>
#include <fstream>
#include <stdlib.h> //for exit()function
using namespace std;
int main()
{
ifstream infile;
double val;
infile.open("datalog");
for (int i=0; i<=20; i++)
{
if(infile >> val){
cout << val << endl;
} else {
cout << "end of file" << endl;
}
}
return 0;
}
输出如下所示:
end of file
end of file
end of file
end of file
end of file
end of file
end of file
end of file
end of file
end of file
end of file
end of file
end of file
end of file
end of file
end of file
end of file
end of file
end of file
end of file
end of file
正如我所期望的那样,它将与 datalog.txt 文件的打印相同。
你能帮我找出错误吗?
谢谢,米林德。