在istream中,我使用cin.getline()接受一些字符并输入EOF信号(因为我的操作系统是MAC,我按下控制+ D)然后第二个cin.getline()接受其余的流。 但是,我测试cin.eof()的值,在第一个cin.getline()之前,第一个和第二个cin.getline()之间,以及最后。 在这个程序中,我都使用EOF信号来终止这三个cin.getline();
代码:
#include<iostream>
using namespace std;
int main()
{
cout<<"now EOF:"<<cin.eof()<<endl;
char character[10];
cout<<"input ten digit character,with '!' in the middle,end with EOF(ctrl+d):"<<endl;
cin.getline(character, 10,'!');
cout<<endl;
cout<<"get the character:"<<endl;
cout<<character<<endl;
char character2[10];
cout<<"now EOF:"<<cin.eof()<<endl;
cout<<"press(ctrl+d):"<<endl;
cin.getline(character2, 10,'!');
//cin>>character2;
cout<<endl;
cout<<character2<<endl;
cout<<"now EOF:"<<cin.eof()<<endl;
}
结果在这里:
now EOF:0
input ten digit character,with '!' in the middle,end with EOF(ctrl+d):
123!456
get the character:
123
now EOF:0
press(ctrl+d):
456
now EOF:1
但是当我用注释的部分cin&gt;&gt; character2代替cin.getline(character2,10,&#39;!&#39;)时:
结果是:
now EOF:0
input ten digit character,with '!' in the middle,end with EOF(ctrl+d):
123!456
get the character:
123
now EOF:0
press(ctrl+d):
456
now EOF:0
我想知道,为什么会发生这种情况以及cin.eof()值如何变化。 谢谢!
答案 0 :(得分:1)
在你的第二个例子中,新行字符仍可供阅读 - 你已经读过一个字符串,而不是一行。
在输出中查看缺少的额外换行符?这就是线索。
但更重要的是,请勿使用eof
。见Why is “while ( !feof (file) )” always wrong?
答案 1 :(得分:0)
如果 <div class="quotation">
<img><img src="/assets/quote.png" alt="Quote sign" /></img>
<blockquote>The tipping point is that magic moment when an idea, trend, or social behavior crosses a threshold, tips, and spreads like wildfire.<p>Malcolm Gladwell</p></blockquote>
</div>
返回menuData = {
"menu": [
{ "id": 0, "name": "DashBoard", "image":"/images/dashboard.gif", "link": "blah.html", "subMenu": [
{ "id": 10, "name": "Running", "image": null, "link": "blah.html", "subMenu": null },
{ "id": 11, "name": "Alarms", "image": null, "link": "blah.html", "subMenu": null },
{ "id": 12, "name": "Moisture", "image": null, "link": "blah.html", "subMenu": [
{ "id": 20, "name": "Overview", "image": null, "link": "blah.html", "subMenu": null },
{ "id": 21, "name": "M. Details", "image": null, "link": "blah.html", "subMenu": null }
]
}
]
},
{ "id": 1, "name": "Programs", "image":"/images/programs_on.gif", "subMenu": [
{ "id": 30, "name": "Intro", "image": null, "link": "programs.html", "subMenu": null },
{ "id": 31, "name": "Pg View", "image": null, "link": "programs.html", "subMenu": null },
{ "id": 32, "name": "Programs", "image": null, "link": "programs.html", "subMenu": [
{ "id": 40, "name": "Program #1", "image": null, "link": "programs.html", "subMenu": [
{ "id": 50, "name": "Start Time", "image": null, "link": "programs.html", "subMenu": null },
{ "id": 51, "name": "Station Time", "image": null, "link": "programs.html", "subMenu": null },
{ "id": 52, "name": "Program Setting", "image": null, "link": "programs.html", "subMenu": null }
]
},
{ "id": 41, "name": "Program #2", "image": null, "link": "programs.html", "subMenu": [
{ "id": 60, "name": "Start Time", "image": null, "link": "programs.html", "subMenu": null },
{ "id": 61, "name": "Station Time", "image": null, "link": "programs.html", "subMenu": null },
{ "id": 62, "name": "Program Setting", "image": null, "link": "programs.html", "subMenu": null }
]
}
]
}
]
}
]};
Object.prototype.getNestedValue = function(...a) {
return a.length > 1 ? (this[a[0]] !== void 0 && this[a[0]].getNestedValue(...a.slice(1))) : this[a[0]];
};
document.write(menuData.getNestedValue("menu",1,"subMenu",2,"subMenu",0,"subMenu",1,"id"));
,则表示上一次输入操作失败,因为该流位于文件的末尾。这就是为什么你不能用它来控制输入循环的原因:当它真实时,输入已经失败了。这就是为什么输入操作本身可以告诉你它们是否成功的原因:只要输入成功,你就可以循环。这段代码
istream::eof()
每次true
调用读取一行时,都会执行循环。当while (cin.getline(whatever)) {
do_something(whatever);
}
到达输入的末尾时,getline
调用将失败,并且循环不会被执行。这段代码:
cin
即使对getline
的调用失败,也会调用while (!cin.eof()) {
cin.getline(whatever);
do_something(whatever);
}
。 在之后调用失败,在下一次循环中,do_something
将返回true并且循环将退出。