stdout and stderr to console and file simultaneously

Discussion in 'C Programming' started by ids, Apr 13, 2009.

  1. ids

    ids Guest


    Is it possible to display the output of a child program on the console
    AND capture it at the same time by redirecting to a file?

    Say I have a Makefile and a program that invokes 'gmake' using:

    01) system("gmake -f Makefile");


    02) system("gmake -f Makefile > build_report.txt");


    03) popen("gmake -f Makefile 2>&1", "r");

    (01) will write the stdout and stderr of 'gmake' to the console. (02)
    allows me to capture the stdout, but it sends only the stderr to the
    console. (03) helps to capture both stdout and stderr, but it hides
    both from the console.

    What I'm looking for is a method to capture both stderr and stdout and
    display to the console at the same time. Is there a way to do that?

    ids, Apr 13, 2009
    1. Advertisements

  2. man tee[/QUOTE]

    Sea cow!!!
    Kenny McCormack, Apr 13, 2009
    1. Advertisements

  3. ids

    ids Guest

    Great! The following command does the job.

    system("gmake -f Makefile 2>&1 | tee build_report.txt");

    ids, Apr 13, 2009
  4. ids

    CBFalconer Guest

    Anthony gave you an off-topic answer that doesn't apply to many
    (most?) systems. It looks as if you are lucky and have a usable
    tee program (or shell command).

    You should have asked this question on a newsgroup dealing with
    your operating system.
    CBFalconer, Apr 14, 2009
  5. He simply asked a "How do I do this?" question. The fact that
    standard C deons't provide a solution doesn't mean that asking it here
    was inappropriate. Probably Anthony Fremont, in answering the
    question, should have suggested a different newsgroup.
    Keith Thompson, Apr 14, 2009
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.