C ++:将ifstream与getline()一起使用; [英] C++: Using ifstream with getline();
本文介绍了C ++:将ifstream与getline()一起使用;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
检查此程序
ifstream filein("Hey.txt");
filein.getline(line,99);
cout<<line<<endl;
filein.getline(line,99);
cout<<line<<endl;
filein.close();
文件Hey.txt中包含很多字符.远超过1000
The file Hey.txt has alot of characters in it. Well over a 1000
但是我的问题是 为什么第二次我尝试打印行.它没有打印出来吗?
But my question is Why in the second time i try to print line. It doesnt get print?
推荐答案
根据C ++参考(此处)getline设置提取了count-1个字符后的ios::fail
.您必须在getline()
个调用之间调用filein.clear();
.
According to the C++ reference (here) getline sets the ios::fail
when count-1 characters have been extracted. You would have to call filein.clear();
in between the getline()
calls.
这篇关于C ++:将ifstream与getline()一起使用;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文