c++ 读取txt文件 最后一个数字读不到 为什么

2025-02-14 04:02:12
推荐回答(1个)
回答1:

当读入第4个数字后,文件就已经到达末尾了,while循环就不成立了。
while (filein.good())
{
cout << getcont << ' ';
filein >> getcont;
}
最简单的方法就是:
//insert the content into the variable
double getcont;
filein >> getcont;

//judge if the file readin is EOF or fail
while (filein.good())
{
cout << getcont << ' ';
filein >> getcont;
}
直接改为:
double getcont;

while (filein>> getcont)
{
cout << getcont << ' ';
}