如何阅读cin与空格直到换行符? [英] How to read cin with whitespace up until a newline character?

查看:136
本文介绍了如何阅读cin与空格直到换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从C ++中从当前位置直到换行符读取字符串。要读取的字符可以包括空格。我的第一个通过失败,因为它停止在第一个空格:

I wish to read from cin in C++ from the current position up until a newline character into a string. The characters to be read may include spaces. My first pass fails because it stops on the first space:

string result;

cin >> result;

如果给予cin:

(cd /my/dir; doSometing)\n

变量结果只有:

(cd

我认为我应该能够使用流操作符来完成这一点,但是skipws不是很对,因为它引发了回车的空格和制表符,加上它听起来像

I would think I should be able to use stream manipulators to accomplish this, but the skipws was not quite right in that it throws carriage returns in with spaces and tabs, plus it sounds like that is for leading whitespace to be skipped.

也许我需要使用streambuf这样的东西?

Perhaps I need to use streambuf something like this?

streambuf buf;

cin >> buf;


推荐答案

std::string str; 
std::getline( std::cin, str);

这篇关于如何阅读cin与空格直到换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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