How to distinguish between stdout and stderr

Discussion in 'C Programming' started by Andre, Jul 20, 2003.

  1. Andre

    Andre Guest

    Hi,

    I have a program that sends some output to stdout and some to stderr. I
    need to separate the two using the command-line so that I direct stderr
    output to a file, say fileA.txt, and stdout output to a file, say
    fileB.txt. I'm trying to implement a program that would then take the
    two files and use them separately. By the way, I'm on Linux. Thanks!

    -Andre
     
    Andre, Jul 20, 2003
    #1
    1. Advertisements

  2. OT.
    But since it's such a small question I'll answer it anyway :p

    #split stderr and stdout into seperate files
    ../myprog 1> stdout.file 2> stderr.file
    #join stderr and stdout into the same file
    ../myprog &> stdout_and_stderr.file
     
    Pieter Droogendijk, Jul 20, 2003
    #2
    1. Advertisements

  3. The command line trick is system-dependent.
    To stay portable, use freopen() at the very beginning of main().

    if (freopen("fileB.txt", "w", stdout) != NULL)
    {
    if (freopen("fileA.txt", "w", stderr) != NULL)
    {
    /* your app. */
    }
    }
     
    Emmanuel Delahaye, Jul 20, 2003
    #3
  4. Andre

    Daniel Haude Guest

    On Sun, 20 Jul 2003 17:22:14 +1000,
    This problem is entirely a shell issue (on Unix), and a trivial one-liner
    as such, but dependent on the shell you're using. It has nothing to do
    with the C programming language -- and therefore doesn't belong in this
    group, even though both the program in question and the shell are likely
    written in C. The nice folks over at comp.unix.programmer will happily
    help you.
     
    Daniel Haude, Jul 20, 2003
    #4
  5. Andre

    Andre Guest

    Hi Pieter,

    Thank you so much :) you helped a great deal!

    regards,

    -Andre
     
    Andre, Jul 20, 2003
    #5
  6. in comp.lang.c i read:
    actually comp.unix.shell would be better.

    programmatically, one might use freopen() to associate different files to
    each stream, from within the program, an issue appropriate for either
    comp.lang.c or comp.unix.programmer, though outside of the initial request.
     
    those who know me have no need of my name, Jul 21, 2003
    #6
  7. Andre

    Dan Pop Guest

    You got it *completely* wrong!

    What makes you think that fileB.txt and fileA.txt are *portable* file
    names? By hardcoding file names in your code, you have given up any
    hope of portability and the program itself is horribly inflexible: what
    if you want to redirect to other files on the next run?

    The right thing is to handle these details from outside the program,
    so that the program's code remains portable and the program itself is
    flexible.

    Dan
     
    Dan Pop, Jul 21, 2003
    #7
  8. Andre

    Dan Pop Guest

    fangorn:~ 1780> ls 1> stdout.file 2> stderr.file
    Ambiguous output redirect.
    fangorn:~ 1781> ls &> stdout_and_stderr.file
    Invalid null command.

    What am I doing wrong? ;-)

    Don't attempt to answer any question, topical or not, if you don't know
    the *complete* answer!

    Dan
     
    Dan Pop, Jul 21, 2003
    #8
    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.