读取整行输入 [英] Reading a full line of input
问题描述
我尝试存储用户通过控制台输入的输入。所以我需要包含enter和任何空格。
I'm trying to store the input that user enters through console. so I need to include the "enter" and any white space.
但 cin
第一个空格。
有一种方法可以读取整行,直到CTRL + Z被按下,或者什么?
Is there a way to read whole lines until CTRL+Z is pressed, or something?
推荐答案
有一种方式像readLines直到CTRL + Z被按下,或者是
is there a way like readLines till CTRL+Z is pressed or something ??
是的,就像这样,使用免费的 std: :getline
函数( istream
Yes, precisely like this, using the free std::getline
function (not the istream
method of the same name!):
string line;
while (getline(cin, line)) {
// do something with the line
}
这将从输入中读取行(包括空格,但不包括结束换行符),直到达到输入结束或 cin
发出错误信号。
This will read lines (including whitespace, but without ending newline) from the input until either the end of input is reached or cin
signals an error.
这篇关于读取整行输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!