从std :: cout或std :: ofstream(file)获取std :: ostream [英] Obtain a std::ostream either from std::cout or std::ofstream(file)

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

问题描述

如何将 std :: ostream 绑定到 std :: cout std :: 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或输出文件流的底层streambuf关联到外部。之后,您可以写入出,最终将在正确的目的地。如果你只是想把所有的东西都转到 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆