我想读取类似这样的数据:
1 1 2 1 1
2 2 3 2 2
3 3 4 3 3
4 4 5 4 4
每读一行会去做一次计算,所以需要逐行读。
我试着用 if (infile.get() !='\n')来判断换行,但是发现这样会吞掉文件的第一个数字,比如第一行会读成1 2 1 1,但是后面是正常的。
求解我这个代码应该如何改?或者如何实现逐行读取文件里数据。
我想读取类似这样的数据:
1 1 2 1 1
2 2 3 2 2
3 3 4 3 3
4 4 5 4 4
每读一行会去做一次计算,所以需要逐行读。
我试着用 if (infile.get() !='\n')来判断换行,但是发现这样会吞掉文件的第一个数字,比如第一行会读成1 2 1 1,但是后面是正常的。
求解我这个代码应该如何改?或者如何实现逐行读取文件里数据。
请参考代码:
#include <string>
#include <fstream>
#include <iostream>
int main()
{
ifstream myfile("C:\\content.txt");
string temp;
if (!myfile.is_open())
{
cout << "未成功打开文件" << endl;
}
while (getline(myfile, temp))
{
for (int i = 0; i < temp.size(); i++)
{
cout << temp[i];
}
cout << endl;
}
myfile.close();
return 0;
}