我想遍历文件并打印所有字符串。例如,文件包含:通过文件迭代失败
a
1
2
10
a1
1
b
将输出:
a
a1
b
我写我的代码是这样的:
int main(){
ifstream stream;
stream.open("example.txt");
string temp;
while (getline(stream, temp)){
cout<<temp<<endl;
int n;
while(stream>>n){}
}
}
这个节目的在线打印 “a” 和 “A1”。和建议?
你不会在'while(stream >> n){}'之后清除失败状态。 – ildjarn 2013-03-14 22:24:19
@ildjarn我用它遍历所有整数 – 2013-03-14 22:34:50
*非常*很好地问问题。 – 2013-03-14 22:50:49