读取整行输入 [英] Reading a full line of input

查看:109
本文介绍了读取整行输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试存储用户通过控制台输入的输入。所以我需要包含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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆