How to read cin with whitespace up until a newline character?

会有一股神秘感。 提交于 2019-12-17 06:17:06

问题


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;

If cin is given:

(cd /my/dir; doSometing)\n

The variable result only gets:

(cd

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.

Perhaps I need to use streambuf something like this?

streambuf buf;

cin >> buf;

回答1:


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


来源:https://stackoverflow.com/questions/1583652/how-to-read-cin-with-whitespace-up-until-a-newline-character

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!