Redirect FROM stderr to another file descriptor
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,