C, UNIX: How to redirect 'stdout' to a file from a C code?

I am looking for a way to redirect standard output to a file from a C-code;
so, any 'cout<<..' or 'printf(...)' will be written into a file.

I have a server source that I need to debug.
That program called by RPC (remote procedure call) and has no any session to print out anything.
I have some my source with set of macro and functions that are useful for debugging any source; but, it is writing into the standard output.

Therefore I would like to redirect the stdout to a file to use those debug-tools into the server source.

How that could be done?

Thanks.

Here is sample code using dup() and dup2()- I copied this from somewhere years ago:

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc, const char *argv[])
{
    int out = open("cout.log", O_RDWR|O_CREAT|O_APPEND, 0600);
    if (-1 == out) { perror("opening cout.log"); return 255; }

    int err = open("cerr.log", O_RDWR|O_CREAT|O_APPEND, 0600);
    if (-1 == err) { perror("opening cerr.log"); return 255; }

    int save_out = dup(fileno(stdout));
    int save_err = dup(fileno(stderr));

    if (-1 == dup2(out, fileno(stdout))) { perror("cannot redirect stdout"); return 255; }
    if (-1 == dup2(err, fileno(stderr))) { perror("cannot redirect stderr"); return 255; }

    puts("doing an ls or something now");

    fflush(stdout); close(out);
    fflush(stderr); close(err);

    dup2(save_out, fileno(stdout));
    dup2(save_err, fileno(stderr));

    close(save_out);
    close(save_err);

    puts("back to normal output");

    return 0;
}

From your description it does not sound like this will help you.

2 Likes

Thank you, jim mcnamara, it is perfectly helps!
Sure here is more than I need in my case, but everything is clear and strait forward!
Additionaly, your code keep a way to switch back that not needed right now, but could be a task later on such approach!

Also I've found useful (by another reply) the 'freopen()' C-function and did it in very simple way, too.
(... for anybody else with the same task and for myself later, here is how I did it with freopen() ) :

#include <iostream>
#include <fstream>
#include <stdio.h>

using namespace std;

void redir(char* fl_nm)
{
  freopen(fl_nm,"a",stdout);
}

int main()
{
   cout << "starting - by cout\n";
   printf(" this written by printf()\n Following messages shoul go to the file '/tmp/tst_redir.log'\n");

   redir((char*)"/tmp/tst_redir.log");

   cout << ".. By 'cout' after using the redir(): this should be written to the file\n";
   printf(" .. By printf() after 'redir()'...\n");

  return 0;
}

But, again, thanks for your solution!!

freopen DOES NOT redirect standard output! You are only redirecting the C file pointer 'stdout', not the file descriptor it uses.

Basically, you're ripping the floor out from under your program without guaranteeing that the Right Thing has happened to put a new floor back. The 'stdout' file pointer will continue to work, but anything not informed of the change might not -- like cout, and any subprocesses you happen to run. Their output may not go where you expected, or go nowhere at all, or crash. It might work right, if the next file opened happens to land at file descriptor 1 -- or it might not. The behavior is undefined and at the mercy of what libraries and compiler you're using. All that's guaranteed after you do that is that stdio routines like printf() will go where you redirected.

Whatever file descriptor 1 went to before, might not be properly closed after freopen, either. If stdout was an open file descriptor to a terminal preventing your ssh window from closing, such is life.

The "right thing" to do if you really want to redirect "standard output", not just the stdio external variable "stdout", is to ensure that the file you want is opened specifically as file descriptor one, which is what dup2() does in the example you were given. Which is much simpler than it looks once you realize it's redirecting twice: Once for stdout, once for stderr. Re-opening onto file descriptor one with dup2() also guarantees that whatever was there before, is forced to close. This could be especially important if that happened to be a terminal or device file.

Also, your example is concerning. Never mix printf and cout, for starters, especially if you're going to play funny games with redirection.