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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:47:12+00:00 2026-05-27T00:47:12+00:00

I am using a library that is printing a warning message to cout or

  • 0

I am using a library that is printing a warning message to cout or cerr. I don’t want this warning message to reach the output of my program. How can I catch this output and put it into /dev/null or similar?

MWE:

#include <iostream>

void foo()
{
    std::cout << "Boring message. " << std::endl;
};

int main()
{
    foo();
    std::cout << "Interesting message." << std::endl;
    return 0;
}

The output should be:

Interesting message.

How should I modify main to get the desired output? (foo must not be changed.)


I tried using freopen() and fclose(stdout) as suggested in this question How can I redirect stdout to some visible display in a Windows Application?. The result is that nothing is printed.

  • 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-27T00:47:13+00:00Added an answer on May 27, 2026 at 12:47 am

    If you are sure that thing does not redirect output (e.g. to /dev/tty/, which would be standard-out again) (which I don’t think), you could redirect before calling them.

    #include <iostream>
    #include <sstream>
    
    void foobar() { std::cout << "foobar!\nfrob!!"; }
    
    int main () {    
        using namespace std;
    
        streambuf *old = cout.rdbuf(); // <-- save        
        stringstream ss;
    
        cout.rdbuf (ss.rdbuf());       // <-- redirect
        foobar();                      // <-- call
        cout.rdbuf (old);              // <-- restore
    
        // test
        cout << "[[" << ss.str() << "]]" << endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using a library that has headers without the .h This defeats visual
Using the D3DX library that is a part of directX, specifically directx9 in this
I can currently see which keys are pressed using JavaScript, using a library that
I have a program that I'm writing where I am using another company's library
I am using a library that prints a bunch of superfluous information to the
I'm using a library that defines an interface: template<class desttype> void connect(desttype* pclass, void
In C#, I am using a library that defines an enum. I would like
I need some help on compiler flags in c++. I'm using a library that
I'm using a third-party library that returns XML that is not valid, because it
I am using a charting javascript library that expects its data in a specific

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.