60

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);

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?

Frank Krueger
  • 69,552
  • 46
  • 163
  • 208
mavam
  • 12,242
  • 10
  • 53
  • 87

6 Answers6

75
std::streambuf * buf;
std::ofstream of;

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

std::ostream out(buf);

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

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;
}

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

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
28

This is exception-safe:

void process(std::ostream &os);

int main(int argc, char *argv[]) {
    std::ostream* fp = &cout;
    std::ofstream fout;
    if (argc > 1) {
        fout.open(argv[1]);
        fp = &fout;
    }
    process(*fp);
}

Edit: Herb Sutter has addressed this in the article Switching Streams (Guru of the Week).

Tom
  • 10,689
  • 4
  • 41
  • 50
  • This doesn't seem any more exception-safe than the original code. – Brian Dec 22 '08 at 20:46
  • Yes it is. The original code has a memory leak if an exception is thrown while processing (*fp << "Hello World" << std::endl). – Tom Dec 23 '08 at 03:58
  • 3
    Why this answer gets my vote is that the first answer breaks an old rule of mine "Don't mess with the internals of other objects". Just because you **can** replace rdbuf does not mean you should. – dex black Feb 28 '11 at 11:06
  • 2
    Herb's code, using the `?:` operator with 2nd and 3rd arguments of different types and lvalue/rvalue-ness, is not valid and does not compile with modern compilers such as Comeau Online or MSVC 10.0. I've mailed him about it. But until it's fixed, perhaps make a note of that in the answer, that the linked-to GOTW code is invalid. Cheers, – Cheers and hth. - Alf Aug 20 '11 at 02:46
8
std::ofstream of;
std::ostream& out = condition ? std::cout : of.open(filename);
Tony Clifton
  • 97
  • 1
  • 2
3

Referencing from this post.

You can apply a similar approach.

struct noop {
    void operator()(...) const {}
};
std::shared_ptr<std::ostream> of;
if (condition) {
    of.reset(new std::ofstream(filename, std::ofstream::out));
} else {
    of.reset(&std::cout, noop());
}
-1

Being a novice to C++, I don't know if this is exception-safe, but here's how I usually do it:

std::ostream& output = (condition)?*(new std::ofstream(filename)):std::cout;
user32849
  • 609
  • 1
  • 6
  • 16
-4

The following simple code works for me:

int main(int argc, char const *argv[]){   

    std::ofstream outF;
    if (argc > 1)
    {
        outF = std::ofstream(argv[1], std::ofstream::out); 
    }

    std::ostream& os = (argc > 1)? outF : std::cout;
}
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574