从iostream读书 [英] Reading from an iostream

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

问题描述

也许我缺少的东西,但我有很多麻烦找到任何有关如何从iostream (std :: iostream& stream)如何读取的信息。有没有反正我可以将它转换为字符串或类似的?

Maybe I'm missing something but I'm having a lot of trouble finding any information on how to how to read from an iostream (std::iostream& stream). Is there anyway I can convert it to a string or similar?

为了澄清这是(我基本上试图做的,例如):

For clarification this is (what I'm basically trying to do, for example):

std::stringstream ss("Maybe I'm missing something \n but I'm having a lot of trouble finding any information on how to how to read from an iostream.");
readStream(ss);

void readStream(std::iostream& stream)
{   
    std::string out;
    stream >> out;
    //do some stuff with the string
}

out 将等于Maybe,而不是完整字符串。

This seems to work but out will be equal to "Maybe" rather than the full string.

推荐答案

您从iostream读取的方式与使用cin时相同。

You read from an iostream the same way you would if you were using cin.

stream >> varName;

疯狂的语法是的,但这是流的决策者决定做的。

Crazy syntax yes, but that's what the makers of streams decided to do.

如果你读到字符串,你也可以使用get和getline。 Get将获得下一个字符或指定的字符缓冲区,getline将转到下一个换行符。

You can also use get and getline if your reading to strings. Get will get the next character or a specified buffer of characters, and getline will go to the next newline.

getline(stringName);

您可以在这里阅读更多: http://cplusplus.com/reference/iostream/iostream/

You can read more on this here: http://cplusplus.com/reference/iostream/iostream/

这篇关于从iostream读书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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