標準出力の出力は通常ディスプレイへ出力されますが、std::cout.rdbuf()関数で、 rdbufを設定する事で、ファイルへ出力させる事も可能です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | #include <tchar.h> #include <iostream> #include <fstream> int _tmain ( int argc , _TCHAR* argv[] ) { std::ofstream ofstr( L ".¥¥test.txt" ); std::streambuf* strbuf; // 変更前の値を取得 strbuf = std::cout.rdbuf( ofstr.rdbuf() ); // 標準出力へ出力(ファイルへ出力されます) std::cout << "標準出力がファイルになった?" << std::endl; // 元に戻す std::cout.rdbuf( strbuf ); // 正常終了 return ( 0 ); } |
標準出力がファイルになった?