weidagang2046的專欄

          物格而后知致
          隨筆 - 8, 文章 - 409, 評論 - 101, 引用 - 0
          數據加載中……

          C++標準I/O重定向

          #include <iostream>
          #include <fstream>
          int main()
          {
          ??? std::ofstream logFile("out.txt");
          ??? std::streambuf *outbuf = std::cout.rdbuf(logFile.rdbuf());
          ??? std::streambuf *errbuf = std::cerr.rdbuf(logFile.rdbuf());

          ??? // do the actual work of the program;
          ??? // GUI code and event loop would go here
          ??? std::cout << "This would normally go to cout but goes to the log file\n";
          ??? std::cerr << "This would normally go to cerr but goes to the log file \n";
          ??? logFile << "This goes to the log file\n";
          ??? // end of program body

          ??? // restore the buffers
          ??? std::cout.rdbuf(outbuf);
          ??? std::cerr.rdbuf(errbuf);
          }

          rdbuf函數返回一個由基類basic_ios管理的流緩沖區的指針。重載版本允許你替換流緩沖區,返回值是原始的流緩沖區。解決方法很簡單—用你的log文件的流緩沖區替換cout和cerr的流緩沖區。程序結束時,改回原來的流緩沖區。

          posted on 2006-11-19 21:38 weidagang2046 閱讀(2050) 評論(0)  編輯  收藏 所屬分類: C/C++

          主站蜘蛛池模板: 凌海市| 富川| 鲁甸县| 且末县| 馆陶县| 麟游县| 五大连池市| 扶余县| 淮滨县| 新沂市| 镇平县| 大英县| 会同县| 韶山市| 富民县| 皮山县| 临潭县| 仁怀市| 栾川县| 南安市| 合水县| 静宁县| 灵武市| 阿坝县| 舒城县| 潞城市| 竹山县| 怀来县| 涡阳县| 共和县| 巴彦淖尔市| 霞浦县| 昭苏县| 武川县| 汉寿县| 华阴市| 林西县| 海兴县| 定陶县| 清新县| 桓台县|