使用EOF获取所有用户输入?
我有问题从终端获取所有用户输入。这总是短的一行,这是最后一行。
do{
getline(std::cin, input);
buffer += input;
buffer += '\n';
}while(!input.empty());
给我所需的输出,但是当我按Enter时结束,当我按CTRL+D时,我希望它到达文件末端(EOF)时结束。我已经尝试过
while(!(std::cin >> input).eof()){
buffer += input;
buffer += '\n';
}
,但没有得到最后一行。在while-loop之后,将输入添加到缓冲区也无法解决。
I'm having problems getting all the user input from the terminal. It's always one line short, and it's the last line.
do{
getline(std::cin, input);
buffer += input;
buffer += '\n';
}while(!input.empty());
gives me the desired output, but it ends when I press Enter, and I want it to end when it reaches End Of File(EOF), when i press Ctrl+D. I have tried
while(!(std::cin >> input).eof()){
buffer += input;
buffer += '\n';
}
but it doesn't get the last line. Adding the input to the buffer after the while-loop also doesn't solve it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
继续阅读输入,
getline
getline:您输入
eof
getline
将设置eofbit
,循环将结束。无论如何,第二片片段也应存储最后一个输入。
Keep reading input with
getline
:When you input
EOF
getline
will seteofbit
and the loop will end.In any case the second snippet should store the last input as well.