C++ 如何将整个 istream 打印到标准输出和字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/675953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to print an entire istream to standard out and string
提问by kal
How do you print an istream variable to standard out. [EDIT] I am trying to debug a scenario wherein I need to ouput an istream to a log file
如何将 istream 变量打印为标准输出。[编辑] 我正在尝试调试一个场景,其中我需要将 istream 输出到日志文件
回答by éric Malenfant
You ouput the istream's streambuf.
您输出 istream 的流缓冲。
For example, to output an ifstream to cout:
例如,输出一个 ifstream 到 cout:
std::ifstream f("whatever");
std::cout << f.rdbuf();
回答by jalf
Edit:I'm assuming that you want to copy the entire contents of the stream, and not just a single value. If you only want to read a single word, check 1800's answer instead.
编辑:我假设您要复制流的全部内容,而不仅仅是单个值。如果您只想阅读一个单词,请查看 1800 的答案。
The obvious solution is a while-loop copying a word at a time, but you can do it simpler, as a nice oneliner:
显而易见的解决方案是一次复制一个单词的 while 循环,但您可以做得更简单,作为一个不错的单行:
#include <iostream>
#include <iterator>
...
std::istream i;
std::copy(std::istream_iterator<char>(i), std::istream_iterator<char>(), std::ostream_iterator<char>(std::cout));
The stream_iterators use operator << and >> internally, meaning they'll ignore whitespace. If you want an exact copy, you can use std::istreambuf_iterator and std::ostreambuf_iterator instead. They work on the underlying (unformatted) stream buffers so they won't skip whitespace or convert newlines or anything.
stream_iterators 在内部使用运算符 << 和 >>,这意味着它们将忽略空格。如果你想要一个精确的副本,你可以使用 std::istreambuf_iterator 和 std::ostreambuf_iterator 代替。它们在底层(未格式化的)流缓冲区上工作,因此它们不会跳过空格或转换换行符或任何东西。
You may also use:
您还可以使用:
i >> std::noskipws;
to prevent whitespace from disappearing. Note however, that if your stream is a binary file, some other characters may be clobbered by the >>
and <<
operators.
以防止空白消失。但是请注意,如果您的流是二进制文件,则>>
and<<
运算符可能会破坏一些其他字符。
回答by rlbond
This will print the whole stream, 1 character at a time:
这将打印整个流,一次 1 个字符:
char c;
c = my_istream.get();
while (my_istream)
{
std::cout << c;
c = my_istream.get();
}
This will print the whole thing, but discard whitespace:
这将打印整个内容,但丢弃空格:
std::string output;
while(my_istream >> output)
std::cout << output;
回答by 1800 INFORMATION
You need to read from it, and then output what you read:
您需要从中读取,然后输出您读取的内容:
istream stm;
string str;
stm >> str;
cout << str;
回答by Shakiba Moshiri
std::ifsream overloading and std::ostringstream
std::ifsream 重载和 std::ostringstream
c++11 or upper
c++11 或更高版本
Pay attention to &&
in std::ifstreamthat allow you to direct using
注意&&
在std::ifstream中允许你直接使用
#include <iostream>
#include <sstream>
#include <fstream>
std::ostream& operator<<(std::ostream& os, std::basic_ostringstream&& iss){
return os<<iss.str();
}
std::ostream& operator<<(std::ostream& os, std::ifstream&& ifs){
return std::cout<<ifs.rdbuf();
}
int main()
{
std::cout<<std::ostringstream("Test ostringstream overloading")<<std::endl;
std::ofstream("fstream.txt")<<"Test fstream overloading"<<std::endl;
std::cout<<std::ifstream("fstream.txt")<<std::endl; // overloading okay
}
output:
输出:
Test ostringstream overloading Test fstream overloading Process returned 0 (0x0) execution time : 0.012 s Press ENTER to continue.
Test ostringstream overloading Test fstream overloading Process returned 0 (0x0) execution time : 0.012 s Press ENTER to continue.