从 iostream 读取
也许我遗漏了一些东西,但是我很难找到有关如何从 iostream (std::iostream& stream) 读取的任何信息。有没有办法可以将其转换为字符串或类似的?
为了澄清这一点(例如,我基本上想做的事情):
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”而不是完整的字符串。
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 a way 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
}
This seems to work, but out
will be equal to "Maybe" rather than the full string.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从 iostream 读取数据的方式与使用 cin 时的方式相同。
是的,疯狂的语法,但这就是流的制造者决定做的。
如果您读取字符串,您还可以使用 get 和 getline。 Get 将获取下一个字符或指定的字符缓冲区,而 getline 将转到下一个换行符。
您可以在这里阅读更多相关内容:http://cplusplus.com/reference/iostream/iostream/
You read from an iostream the same way you would if you were using cin.
Crazy syntax yes, but that's what the makers of streams decided to do.
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.
You can read more on this here: http://cplusplus.com/reference/iostream/iostream/
流会自动转换为它们要转换到的类型。
当转移到字符串时,它会读取到第一个单词分隔符,您可能希望使用 std::获取线路。
Streams converts automatically for the type they are shifting to.
When shifting to a string, it reads until the first word delimiter, you may wish to use std::getline.
也许您想阅读整行。在这种情况下,您必须使用
std::getline
,因此具有:您还可以通过将其作为第三个参数传递给 std::getline 来选择行分隔符。
Maybe you want to read whole lines. In this case you have to use
std::getline
, thus having:You can also choose a line delimiter character, by passing it to
std::getline
as a third parameter.流运算符>>用于读取格式化的空白分隔文本。
不幸的是 std::string (和 C-Strings)不是对称的(输入/输出不以相同的方式工作(与其他基本类型不同))。当您写入它们时,它们会写入完整的字符串(直到 C 字符串的空终止符“\0”)。
但与大多数语言一样,您可以循环读取流,直到完成。
或者一次一行一行地做同样的事情:
注 1:我提到上面用空格分隔。空白包括空格/制表符,最重要的是换行符,因此使用运算符>>上面它将一次读取一个单词,直到文件末尾,但忽略新行。
注2:运算符>>应该用于格式化文本。因此,它的第一个操作是删除前缀空白字符。在第一个非空白文本上,根据输入类型解析输入,并在与该类型不匹配的第一个字符处停止(这包括空白)。
The stream operator >> is used to read formatted white space separated text.
Unfortunately std::string (and C-Strings) are not symmetric (input/output do not work in the same way (unlike the other fundamental types)). When you write them they write the full string (up to the null terminator, '\0', of the C-string).
But like most languages, you can loop reading the stream until it is finished.
Or the same thing one line at a time:
Note 1: I mentioned white space separated above. White space includes space/tab and most importantly new line so using the operator >> above it will read one word at a time until the end of file, but ignore new line.
Note 2: The operator >> is supposed to be used on formatted text. Thus its first action is to drop prefix white space characters. On the first non white space text, parse the input as appropriate for the input type and stop on the first character that does not match that type (this includes white space).