<分区>
我使用以下代码读取其中包含名字和姓氏的文件。
名字姓氏
名字姓氏
do
{ in >> tmp2;
cout << tmp2;
} while(tmp2 != '\n');
然而,这并没有检测到行尾,所以我无法前进,因为我得到了一个无限循环。注意 tmp2 是一个字符。
我该如何解决这个问题。
标签 c++
<分区>
我使用以下代码读取其中包含名字和姓氏的文件。
名字姓氏
名字姓氏
do
{ in >> tmp2;
cout << tmp2;
} while(tmp2 != '\n');
然而,这并没有检测到行尾,所以我无法前进,因为我得到了一个无限循环。注意 tmp2 是一个字符。
我该如何解决这个问题。
最佳答案
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream fin("file");
string first, last, comment;
while (fin >> first >> last) {
cout << first << ' ' << last << endl;
getline(fin, comment); // get the rest annoying strings
}
fin.close();
return 0;
}
关于C++ - 读取文件行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15798666/