c++ - Changing devices to which predefined stream objects are connected -
By default, when a C ++ application where the headers are included, the following 4 stream objects are sent immediately, cin,
Cout, cerr and pause (related extended character types as well). By default cin is connected to the standard input device, which is usually the keyboard and cout, cerr and pause Standard output is connected to the device which is usually the console My question is how can we change the equipment, it is connected to the predefined section objects? Yes, this is possible and it does not depend on operating system. There is a quick demo, Note that a rule As it is, I'd recommend against it. It is usually very clear (for an example) to move the code into a function that refers to an ostream as its parameter, then the necessary ostream or Pass offstream as needed, if you have (example ) Is a large part of the existing code that has already read / writes / coded standard streams, so it can give this job without writing it to everyone. < / Html> cout is redirected to a file:
#include & lt; Iostream & gt; # Include & lt; Fstream & gt; Int main () {// Make a file buff, and open it on a file named "your_output.txt": std :: basic_filebuf & lt; Char, std :: char_traits & lt; Char & gt; & Gt; B; B Open ("your_output.txt", std :: ios_base :: out); // Connect `std :: cout` to the file selected through our file_books: std :: cout.rdbuf (& amp; b); // Write some output (which should not be visible on the screen. Std :: cout & lt; "This is some junk"; 0;}
Comments
Post a Comment