Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8453243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:43:09+00:00 2026-06-10T11:43:09+00:00

I have a program in c++, during the program i use : static ofstream

  • 0

I have a program in c++, during the program i use :

static ofstream s_outF(file.c_str());
if (!s_outF)
{
    cerr << "ERROR : could not open file " << file << endl;
    exit(EXIT_FAILURE);
}
cout.rdbuf(s_outF.rdbuf());

Meaning i redirect my cout to a file.
What would be the easiest way to return the cout back to the standard output?

thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:43:11+00:00Added an answer on June 10, 2026 at 11:43 am

    Save the old streambuf before you change cout‘s streambuf :

    auto oldbuf = cout.rdbuf();  //save old streambuf
    
    cout.rdbuf(s_outF.rdbuf());  //modify streambuf
    
    cout << "Hello File";        //goes to the file!
    
    cout.rdbuf(oldbuf);          //restore old streambuf
    
    cout << "Hello Stdout";      //goes to the stdout!
    

    You can write a restorer to do that automatically as:

    class restorer
    {
       std::ostream   & dst;
       std::ostream   & src;
       std::streambuf * oldbuf;
    
       //disable copy
       restorer(restorer const&);
       restorer& operator=(restorer const&);
      public:   
       restorer(std::ostream &dst,std::ostream &src): dst(dst),src(src)
       { 
          oldbuf = dst.rdbuf();    //save
          dst.rdbuf(src.rdbuf());  //modify
       }
      ~restorer()
       {
          dst.rdbuf(oldbuf);       //restore
       }
    };
    

    Now use it based on scope as:

    cout << "Hello Stdout";      //goes to the stdout!
    
    if ( condition )
    {
       restorer modify(cout, s_out);
    
       cout << "Hello File";     //goes to the file!
    }
    
    cout << "Hello Stdout";      //goes to the stdout!
    

    The last cout would output to stdout even if condition is true and the if block is executed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C# program that needs to spawn a .BAT command file during
I have a program which creates certain save files during its use. Technically they
I have a C++ program which, during execution, will allocate about 3-8Gb of memory
I have below five Integer variables and during the program, they are assigned some
I have used a static global variable and a static volatile variable in file
I have tried to use the program described here Problems in deleting a Folder
Hey everyone. I have an exe file that runs a console program that was
I have a program that updates a config file. For example, the config file
i would like to have program a timer that will count the seconds during
I have Winforms application that must create write to certain configuration file during usage.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.