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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:46:41+00:00 2026-05-20T05:46:41+00:00

My program calls library functions which print to stderr. I want to intervene so

  • 0

My program calls library functions which print to stderr. I want to intervene so that all write calls to file descriptor #2 will instead get sent to somewhere else.

Here is my first attempt:

bool redirect_stderr (int fd)
{
    return dup2 (2, fd) > 0;
}

Here, fd was successfully obtained from open("/foo/bar",O_APPEND|O_CREAT)

After this function returns true, std::cerr<<"blah" goes to the terminal and not to the file.

What am I doing wrong?

Thanks.

UPDATE

Thanks, larsmans, but I’m not there yet…

void redirect_stderr_to (const char * output_file)
{
    int fd = open (output_file, O_APPEND | O_CREAT, S_IRUSR | S_IWUSR);

    if (fd < 0) {
        throw RUNTIME_ERROR;
    }
    else {
        if (-1 == dup2 (fd, STDERR_FILENO))
            throw RUNTIME_ERROR;

        std :: cout << (std::cerr ? "Fine\n" : "Bad\n");
        char x [100];
        std :: cerr
            << "Output to " << getcwd (x, 100) << " / " << output_file
            <<  " yields " << fd << " errno: " << errno << "\n";
        std :: cout << (std::cerr ? "Fine\n" : "Bad\n");
    }
}

This outputs

Fine
Bad

to stdout and the given file is empty. (It is correctly created if it doesn’t exist.)

  • 1 1 Answer
  • 1 View
  • 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-20T05:46:41+00:00Added an answer on May 20, 2026 at 5:46 am

    You reversed the arguments: it’s

    dup2(from_fd, to_fd)
    

    i.e.

    dup2(fd, 2)
    

    (see POSIX.2008 or your manpages.)

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

Sidebar

Related Questions

the following is a line from a python program that calls the demo.exe file.
I have a shim library (shared, C++) which calls functions in another shared library
I have a python program which calls a C function. I send some structures
I have written a c# program that calls a c++ dll that echoes the
I have a program (NWShader) which hooks into a second program's OpenGL calls (NWN)
I have a program that does recursive calls for 2 billion times and the
I have getRuntime().exec() calls in my program; however, two of them will not work:
I've a bash script that sequentially calls a java program. That's a bit tricky
I'm writing a small JavaScript program that makes web calls to pull up weather
I'm trying to fix an existing C-program with VS2005 that eventually calls int system(command)

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.