22

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

kal
  • 28,545
  • 49
  • 129
  • 149

5 Answers5

28

You ouput the istream's streambuf.

For example, to output an ifstream to cout:

std::ifstream f("whatever");
std::cout << f.rdbuf();
Éric Malenfant
  • 13,938
  • 1
  • 40
  • 42
12

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.


The obvious solution is a while-loop copying a word at a time, but you can do it simpler, as a nice oneliner:

#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.

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.

CygnusX1
  • 20,968
  • 5
  • 65
  • 109
jalf
  • 243,077
  • 51
  • 345
  • 550
7

This will print the whole stream, 1 character at a time:

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;
rlbond
  • 65,341
  • 56
  • 178
  • 228
1

You need to read from it, and then output what you read:

istream stm;
string str;
stm >> str;
cout << str;
1800 INFORMATION
  • 131,367
  • 29
  • 160
  • 239
0

std::ifsream overloading and std::ostringstream

c++11 or upper


Pay attention to && in std::ifstream that allow you to direct using

#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.
Shakiba Moshiri
  • 21,040
  • 2
  • 34
  • 44
  • [basic_ostringstream](https://en.cppreference.com/w/cpp/io/basic_ostringstream) is a template dependent function. The first function should be converted to be"template std::ostream& operator<<(std::ostream& os, std::basic_ostringstream&& iss){ return os< – Liu Hao Cheng Mar 20 '19 at 00:17