Results 1 to 6 of 6

Thread: C: capturing stdout

  1. #1
    Join Date
    Apr 2009
    Location
    Germany
    Beans
    2,134
    Distro
    Ubuntu Development Release

    C: capturing stdout

    I want to write a testcase testing the output of a function printing to stderr.
    for that I somehow need to get the stderr from within the same process.
    I also want to avoid having to execute the testcase from a wrapperscript.

    I found you can reopen stderr to a filestream with freopen but it seems impossible to reattach the output to stderr again in a portable way.
    Unfortunately I would like to do this too.

    is there some way I'm overlooking to capture stderr and resetting it in C, without changing the outputting functions code or using a wrapper program around the testcase?

    pseudo code I'm looking for:
    Code:
    to_test():
      print "output"
    
    test_function():
      file = freopen("file", stderr)
      to_test();
      assert(compare_with_golden_file(file, golden))
      reset_stderr

  2. #2
    Join Date
    Aug 2007
    Location
    127.0.0.1
    Beans
    1,800
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: C: capturing stdout

    Wait? In C? Why are you using python in the example?

    EDIT: Oh... pseudocode, sorry about that.
    "Just in terms of allocation of time resources, religion is not very efficient. There's a lot more I could be doing on a Sunday morning."
    -Bill Gates

  3. #3
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: C: capturing stdout

    If I read your post correctly, you want to direct stderr messages to a file, for a temporary amount of time, and then restore everything to 'normal' so that future stderr-directed messages are displayed on the console, as they normally would.

    Well, here's some 'cheesy' code that does this, and it should be portable:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
       FILE* errFile = fopen("myErrors", "w+");
       FILE* save_stderr = stderr;
    
       stderr = errFile;
    
       // this will go to the file
       fprintf(stderr, "This is my first error message.\n");
    
       fclose(errFile);
    
       stderr = save_stderr;
    
       // this will go to the terminal
       fprintf(stderr, "This is my second error message.\n");
    
       return 0;
    }

  4. #4
    Join Date
    Apr 2009
    Location
    Germany
    Beans
    2,134
    Distro
    Ubuntu Development Release

    Re: C: capturing stdout

    are you sure this is portable?
    according this not:
    http://c-faq.com/stdio/undofreopen.html
    and it says it is impossible in the way in want it
    Last edited by MadCow108; January 28th, 2010 at 12:04 PM.

  5. #5
    Join Date
    Jan 2010
    Beans
    1

    Re: C: capturing stdout

    Take a look into dup() and dup2(). They will allow you to duplicate a file descriptor. From here you should be able to redirect the output of stderr to somewhere else for testing.

    Edit: Apologies, I didn't realise the C-Faq you linked explicitly mentioned dup and dup2.
    Last edited by kzmd; January 28th, 2010 at 02:35 PM.

  6. #6
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: C: capturing stdout

    Quote Originally Posted by MadCow108 View Post
    are you sure this is portable?
    according this not:
    http://c-faq.com/stdio/undofreopen.html
    and it says it is impossible in the way in want it
    Works under Linux and Mac OSX. I'm not sure if I care if it works anywhere else. As I have mentioned in the past, I hold ******* in low regard; it is a great multi-media center, but useless for anything else.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •