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

  • Home
  • SEARCH
  • 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 6356171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:54:48+00:00 2026-05-24T22:54:48+00:00

In a C++ program, we have 3 streams: stdin , stdout , and stderr

  • 0

In a C++ program, we have 3 streams: stdin, stdout, and stderr. Can I override these in a console application and use them in a application that uses forms?

For example, if in some base class, I have cout<< "...", can I “redirect” to something visual (like a Windows Form)?

  • 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-05-24T22:54:49+00:00Added an answer on May 24, 2026 at 10:54 pm

    What I would recommend doing is having a class which wraps around an iostream like this :

    #include <iostream>
    #define LOG Log()
    
    class Log
    {
       public:
          Log(){}
          ~Log()
          {
             // Add an newline.
             std::cout << std::endl;
          }
    
    
          template<typename T>
          Log &operator << (const T &t)
          {
             std::cout << t;
             return * this;
          }
    };
    

    Then, whenever you want to change where the data goes, you just change the class behavior.
    Here is how you use the class:

     LOG << "Use this like an iostream.";
    

    [edit]
    As potato swatter suggested, I’ll add an example with something other than cout:

    #include <sstream>
    #define LOG Log()
    
    // An example with a string stream.
    class Log
    {
       private:
          static std::stringstream buf;
       public:
          Log(){}
          ~Log()
          {
             // Add an newline.
             buf << std::endl;
          }
    
    
          template<typename T>
          Log &operator << (const T &t)
          {
             buf << t;
             return * this;
          }
    };
    
    // Define the static member, somewhere in an implementation file.
    std::stringstream Log::buf;
    

    As for why you should try this instead of inheriting from something like a string stream, mainly because you can easily change where the Logger outputs to dynamically. For instance, you could have three different output streams, and use a static member variable to swap between at runtime:

    class Log
    {
       private:
          static int outputIndex = 0;
          // Add a few static streams in here.
          static std::stringstream bufOne;
          static std::stringstream bufTwo;
          static std::stringstream bufThree;
       public:
          // Constructor/ destructor goes here.
    
          template<typename T>
          Log &operator << (const T &t)
          {
             // Switch between different outputs.
             switch (outputIndex)
             {
                case 1:
                   bufOne << t;
                   break;
                case 2:
                   bufTwo << t;
                case 3:
                   bufThree << t;
                default:
                   std::cout << t;
                   break;
             }
             return * this;
          }
    
          static void setOutputIndex(int _outputIndex)
          {
              outputIndex = _outputIndex;
          }
    };
    
    // In use
    LOG << "Print to stream 1";
    Log::setOutputIndex(2);
    LOG << "Print to stream 2";
    Log::setOutputIndex(3);
    LOG << "Print to stream 3";
    Log::setOutputIndex(0);
    LOG << "Print to cout";
    

    This can easily be expanded to create a powerful way of dealing with logging. You could add filestreams, use std::cerr, etc.

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

Sidebar

Related Questions

I have program, that must interact with a console program before my program can
I have a simple plugin architecture for my program that uses an interface to
In my program I have saveral pipes which are connected to stdout and stderr
In a Delphi 7 console application, how can I check whether stdin holds a
I have a Windows C program that gets its data through a redirected stdin
I have program that has a variable that should never change. However, somehow, it
I have program that runs fast enough. I want to see the number of
in a C program I have an long* that I want to serialize (thus
In an embedded program I have a screen object that needs to manage a
with my RCP program I have the problem that I want to have more

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.