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++

          主站蜘蛛池模板: 明星| 镇安县| 迭部县| 玉田县| 双辽市| 汉中市| 汾阳市| 沂源县| 临湘市| 蒲城县| 连南| 承德县| 长武县| 镇安县| 绵竹市| 黄陵县| 兴文县| 曲靖市| 巨野县| 晋城| 伊宁县| 威远县| 通榆县| 迁安市| 蓬溪县| 濉溪县| 乐都县| 嵊州市| 同德县| 武义县| 琼结县| 阿荣旗| 始兴县| 奎屯市| 唐河县| 双桥区| 台山市| 普格县| 禄丰县| 清新县| 长宁县|