Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C++ redirect cout

Options
  • 13-03-2007 6:52pm
    #1
    Registered Users Posts: 4,701 ✭✭✭


    Hi,
    When I use "cout" to print data to the command line I'd like to be able to print it to a text file aswell as a record.
    Does anyone know how to do this? The only thing I can find is a command "rdbuf" used with "cout" but I can't get it to work. My only alternative is to print everything once with "cout" and once again to the file but that just messy and requires twice the code.
    Any help would be much appreciated.

    Bacchus


Comments

  • Registered Users Posts: 151 ✭✭viboy


    From memory something like :
    [PHP]std::ofstream out("output.txt");
    out.rdbuf(std::cout.rdbuf());
    [/PHP]

    will probably work for you.

    If that doesn't work PM me and I'll send you some working code.


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    Write a function called "PrintString" and send it the string you want to print like this (psuedo code):

    void PrintString(string mystring)
    {
    // use cout or whatever
    printToScren(mystring);
    printToFile(mystring);
    }

    So every time you want to display something on the screen *and* in a file, send the string to that function.


  • Registered Users Posts: 4,701 ✭✭✭Bacchus


    hey,
    Thanks for the tips. What I have now is this..
    char LOG_FILE_NAME[30];
    sprintf(LOG_FILE_NAME,"FILES/log_file.txt");
    
    ofstream LOG_FILE;
    LOG_FILE.open(LOG_FILE_NAME,ios::out ); 
    
    streambuf* sbuf = cout.rdbuf();
    
    cout.rdbuf(LOG_FILE.rdbuf());
    cout << "This is sent to file" << endl;
    cout.rdbuf(sbuf);
    cout << "This is sent to prompt" << endl;
    

    That works okay now but do you know how I can get it to write to both the prompt and the file in the one command.

    Thanks,
    Bacchus


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    Bacchus wrote:
    That works okay now but do you know how I can get it to write to both the prompt and the file in the one command.

    And...
    Write a function called "PrintString" and send it the string you want to print like this (psuedo code):

    void PrintString(string mystring)
    {
    // use cout or whatever
    printToScreen(mystring);
    printToFile(mystring);
    }

    You should be able to figure it from there


  • Registered Users Posts: 4,701 ✭✭✭Bacchus


    Cheers Mutant_Fruit, I did it the way you suggested aswell. I was just wondering was there some neat command that uses rdbuf(or something similar) to print to both.


  • Advertisement
  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    Bacchus wrote:
    Cheers Mutant_Fruit, I did it the way you suggested aswell. I was just wondering was there some neat command that uses rdbuf(or something similar) to print to both.
    C++ may have something to do that, but i don't know ;)

    Just a FYI: C# does have something like that. Basically there is a "TraceListener" class to which you can add any number of streams. Then you can do something like Trace.WriteLine("My big long line of text"); and that long line of text will be written to each stream that was added to the TraceListener class. It could be a file, your standard console, a socket, whatever.

    This makes it easy to have one line of debug text to be sent to many places, exactly what you want :P


  • Registered Users Posts: 4,701 ✭✭✭Bacchus


    Just a FYI: C# does have something like that. Basically there is a "TraceListener" class to which you can add any number of streams. Then you can do something like Trace.WriteLine("My big long line of text"); and that long line of text will be written to each stream that was added to the TraceListener class. It could be a file, your standard console, a socket, whatever.

    This makes it easy to have one line of debug text to be sent to many places, exactly what you want :P

    Exactly what I want. I'll look into that, see if I can find anything. Thanks.


Advertisement