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 8217743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:33:24+00:00 2026-06-07T12:33:24+00:00

By default when a c++ application where the header is included, the following 4

  • 0

By default when a c++ application where the header is included, the following 4 stream objects are instantiated, cin, cout, cerr and clog(corresponding wide character types are also instantiated).

Now by default cin is connected to the standard input device which is usually the keyboard and cout, cerr and clog are connected to the standard output device which is usually the console.

My question is how we can change the devices these predefined stream objects are connected to ?

  • 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-07T12:33:26+00:00Added an answer on June 7, 2026 at 12:33 pm

    Yes, it is possible and no it doesn’t depend on the operating system. Here’s a quick demo, redirecting cout to write to a file:

    #include <iostream>
    #include <fstream>
    
    int main() { 
        // create a file buf, and open it on a file named "your_output.txt":
        std::basic_filebuf<char, std::char_traits<char> > b;
        b.open("your_output.txt", std::ios_base::out);
    
        // connect `std::cout` to the chosen file via our file_buf:
        std::cout.rdbuf(&b);
    
        // write some output (which shouldn't show up on screen.
        std::cout << "This is some junk";
        return 0;
    }
    

    Note that as a rule, I’d recommend against this though. It’s generally much cleaner (for one example) to move the code into a function that takes a reference to an ostream as its parameter, then pass the appropriate ostream or ofstream as needed. On the other hand, if you have (for example) a big chunk of existing code that already has reads/writes from/to standard streams coded in, this can let that work without rewriting it all.

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

Sidebar

Related Questions

I have an application layout in my rails app to give a default header/footer
Please consider the following context from Innate : # Default application for Innate def
I strongly need to override JSF 2.0 Content-Type header. Default it is Content-Type:application/xhtml+xml; charset=UTF-8
Is it possible to auto-rotate the default application image according to current landscape mode?
Suppose my solution have two project: myApp: silverlight application project: the default application App
So when I deployed the default guestbook application I get a server error. When
I implemented a simple sitemap class using Django's default sitemap application. As it was
I'm currently looking for a way to launch the default browser application on Android
I'm trying to learn android system and was looking into standard (default) DescClock application
By default in windows application setting are saved in this directory: %USERPROFILE%\Local Settings\Application Data\<Company

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.