cout is an object of the ostream class.
Also, when we write:
filebuf objFileBuf;
objFileBuf.open ("test.txt", ios :: out);
ostream objOstream (&objFileBuf);
objOstream << "Test sentence\n";
objFileBuf.close();
the text gets written to the file.
How to specify the “console” as a file to write through ostream?
You can do this:
Or you can do this:
The second one doesn’t work with streambuf, rather you just create a reference to the
std::cout, and use the reference.