• <legend id='PqJBy'><style id='PqJBy'><dir id='PqJBy'><q id='PqJBy'></q></dir></style></legend>
    1. <tfoot id='PqJBy'></tfoot>
    2. <small id='PqJBy'></small><noframes id='PqJBy'>

      <i id='PqJBy'><tr id='PqJBy'><dt id='PqJBy'><q id='PqJBy'><span id='PqJBy'><b id='PqJBy'><form id='PqJBy'><ins id='PqJBy'></ins><ul id='PqJBy'></ul><sub id='PqJBy'></sub></form><legend id='PqJBy'></legend><bdo id='PqJBy'><pre id='PqJBy'><center id='PqJBy'></center></pre></bdo></b><th id='PqJBy'></th></span></q></dt></tr></i><div id='PqJBy'><tfoot id='PqJBy'></tfoot><dl id='PqJBy'><fieldset id='PqJBy'></fieldset></dl></div>

          <bdo id='PqJBy'></bdo><ul id='PqJBy'></ul>

      1. 从 std::cout 或 std::ofstream(file) 获取 std::ostream

        时间:2023-08-02

          1. <tfoot id='93gnv'></tfoot>
            <i id='93gnv'><tr id='93gnv'><dt id='93gnv'><q id='93gnv'><span id='93gnv'><b id='93gnv'><form id='93gnv'><ins id='93gnv'></ins><ul id='93gnv'></ul><sub id='93gnv'></sub></form><legend id='93gnv'></legend><bdo id='93gnv'><pre id='93gnv'><center id='93gnv'></center></pre></bdo></b><th id='93gnv'></th></span></q></dt></tr></i><div id='93gnv'><tfoot id='93gnv'></tfoot><dl id='93gnv'><fieldset id='93gnv'></fieldset></dl></div>

                <tbody id='93gnv'></tbody>
              <legend id='93gnv'><style id='93gnv'><dir id='93gnv'><q id='93gnv'></q></dir></style></legend>

              <small id='93gnv'></small><noframes id='93gnv'>

                <bdo id='93gnv'></bdo><ul id='93gnv'></ul>

                  本文介绍了从 std::cout 或 std::ofstream(file) 获取 std::ostream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  如何将 std::ostream 绑定到 std::coutstd::ofstream 对象,具体取决于某个程序条件?尽管由于多种原因这无效,但我想实现语义上等同于以下内容的内容:

                  how do I bind a std::ostream to either std::cout or to an std::ofstream object, depending on a certain program condition? Although this invalid for many reasons, I would like to achieve something that is semantically equivalent to the following:

                  std::ostream out = condition ? &std::cout : std::ofstream(filename);
                  

                  我见过一些不是异常安全的例子,例如来自 http://www2.roguewave.com/support/docs/sourcepro/edition9/html/stdlibug/34-2.html:

                  I've seen some examples that are not exception-safe, such as one from http://www2.roguewave.com/support/docs/sourcepro/edition9/html/stdlibug/34-2.html:

                  int main(int argc, char *argv[])
                  {
                    std::ostream* fp;                                           //1
                    if (argc > 1)
                       fp = new std::ofstream(argv[1]);                         //2
                    else
                       fp = &std::cout                                          //3
                  
                    *fp << "Hello world!" << std::endl;                         //4
                    if (fp!=&std::cout) 
                       delete fp;
                  }
                  

                  有人知道更好的异常安全解决方案吗?

                  Does anyone know a better, exception-safe solution?

                  推荐答案

                  std::streambuf * buf;
                  std::ofstream of;
                  
                  if(!condition) {
                      of.open("file.txt");
                      buf = of.rdbuf();
                  } else {
                      buf = std::cout.rdbuf();
                  }
                  
                  std::ostream out(buf);
                  

                  这将 cout 或输出文件流的底层流缓冲与 out 相关联.之后,您可以写入out",它将最终到达正确的目的地.如果您只是希望 std::cout 的所有内容都进入一个文件,您也可以这样做

                  That associates the underlying streambuf of either cout or the output file stream to out. After that you can write to "out" and it will end up in the right destination. If you just want that everything going to std::cout goes into a file, you can aswell do

                  std::ofstream file("file.txt");
                  std::streambuf * old = std::cout.rdbuf(file.rdbuf());
                  // do here output to std::cout
                  std::cout.rdbuf(old); // restore
                  

                  第二种方法的缺点是它不是异常安全的.您可能想编写一个使用 RAII 执行此操作的类:

                  This second method has the drawback that it's not exception safe. You possibly want to write a class that does this using RAII:

                  struct opiped {
                      opiped(std::streambuf * buf, std::ostream & os)
                      :os(os), old_buf(os.rdbuf(buf)) { }
                      ~opiped() { os.rdbuf(old_buf); }
                  
                      std::ostream& os;
                      std::streambuf * old_buf;
                  };
                  
                  int main() {
                      // or: std::filebuf of; 
                      //     of.open("file.txt", std::ios_base::out);
                      std::ofstream of("file.txt");
                      {
                          // or: opiped raii(&of, std::cout);
                          opiped raii(of.rdbuf(), std::cout);
                          std::cout << "going into file" << std::endl;
                      }
                      std::cout << "going on screen" << std::endl;
                  }
                  

                  现在,无论发生什么,std::cout 都处于干净状态.

                  Now, whatever happens, std::cout is in clean state.

                  这篇关于从 std::cout 或 std::ofstream(file) 获取 std::ostream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Windows/C++:是否可以找到引发异常的代码行,并具 下一篇:为什么 C++ STL iostream 不是“异常友好的"?

                  相关文章

                  最新文章

                    <legend id='PDuLz'><style id='PDuLz'><dir id='PDuLz'><q id='PDuLz'></q></dir></style></legend>

                    <tfoot id='PDuLz'></tfoot>
                      <bdo id='PDuLz'></bdo><ul id='PDuLz'></ul>

                    <small id='PDuLz'></small><noframes id='PDuLz'>

                    1. <i id='PDuLz'><tr id='PDuLz'><dt id='PDuLz'><q id='PDuLz'><span id='PDuLz'><b id='PDuLz'><form id='PDuLz'><ins id='PDuLz'></ins><ul id='PDuLz'></ul><sub id='PDuLz'></sub></form><legend id='PDuLz'></legend><bdo id='PDuLz'><pre id='PDuLz'><center id='PDuLz'></center></pre></bdo></b><th id='PDuLz'></th></span></q></dt></tr></i><div id='PDuLz'><tfoot id='PDuLz'></tfoot><dl id='PDuLz'><fieldset id='PDuLz'></fieldset></dl></div>