Thread: Redirect stdout?

  1. #1
    Evil Member
    Join Date
    Jan 2002
    Posts
    638

    Redirect stdout?

    Is there any mechanism for trapping stdout to a file under Win98? A language feature or something I could do to the console?

  2. #2
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    n/m found it.

  3. #3
    Evil Member
    Join Date
    Jan 2002
    Posts
    638
    For those of you interested:

    Code:
    /*
    Info and includes
    */
    
    #define STDOUT_TO_FILE "outfile.txt"
    
    /*
    declarations and anything else
    */
    
    int main () {
       #ifdef STDOUT_TO_FILE
          FILE* newoutput = freopen(STDOUT_TO_FILE, "w", stdout );
          if (!newoutput) {
             cerr << "Failed to redirect stdout";
             return (1);
          }
       #endif
    
    /* 
    body of main()
    */
    
       #ifdef STDOUT_TO_FILE
          fclose(newoutput);
       #endif
    
       return 0;
    }
    Everything sent to cout will be sent to the file identified in the #define. Any other output will be unaffected.

    Comment out the define and all is as it was, the other stuff isn't even processed.

    This would probably not work with things like conio.h and anything that writes directly to the video memory.

    Dunno if this is faqworthy or not, but definitely useful information.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Redirect stdout
    By GoLuM83 in forum C Programming
    Replies: 6
    Last Post: 12-15-2006, 04:17 PM
  2. redirect stdout to editbox
    By kerm1t in forum Windows Programming
    Replies: 1
    Last Post: 05-11-2005, 10:32 PM
  3. Problems with switch()
    By duvernais28 in forum C Programming
    Replies: 13
    Last Post: 01-28-2005, 10:42 AM
  4. Who to redirect stdout to a file in C
    By edugarcia in forum Linux Programming
    Replies: 3
    Last Post: 10-01-2004, 12:18 AM
  5. redirect stdout to a file?
    By Brian in forum C Programming
    Replies: 5
    Last Post: 01-15-2002, 01:06 PM