Redirecting stdout, stderr and stdin

This is a discussion on Redirecting stdout, stderr and stdin within the Linux Programming forums, part of the Platform Specific Boards category; How can I redirect sandard IO handlers of program A, which I have Spawnl:ed from my main program?...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    84

    Redirecting stdout, stderr and stdin

    How can I redirect sandard IO handlers of program A, which I have Spawnl:ed from my main program?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,494
    open()
    dup()
    close()

    Read about those functions in the manual pages.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    This is what I do in C:

    Code:
    #include <stdio.h>
    
    freopen( "stdin.log", "w", stdin );
    freopen( "stdout.log", "w", stdout );
    freopen( "stderr.log", "w", stderr );
    Remember too,
    w+,w - If you want your older contents from the log files to be discarded. Creates text file if not already there.
    r+ - If you want to start writing in the logs from the beginning of the file. Opens for update so the text files must already exist.
    a+,a - If you want to start writing the logs from the end of the file. Text files must already exist.

    This is an example:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main( void )
    {
    	/* Variables 
    	in=UserINPUT */ 
    	char *in = malloc( 20 * sizeof( char ) );
    	
    	/* Setup the streams */
    	/*freopen( "stdin.log", "w", stdin ); This messes up fgets :( */ 
    	freopen( "stderr.log", "w", stderr );
    	freopen( "stdout.log", "w", stdout );
    	
    	/* Send the streams some text */ 
    	fprintf( stdout, "This is stdout\n" ); 
    	fprintf( stderr, "This is stderr\n" );
    	/*fprintf( stdin, "This should not work" ); This messes up fgets :( */ 
    	
    	/* Send stdin some good ol' user text */ 
    	fgets( in, 20, stdin );
    	
    	/* Echo out user input */ 
    	puts( in );
    	
    	/* Return success */ 
    	return 0;
    }
    This is the output:
    Code:
    psycho@Shiva:~/Programming/Laboratory$ gcc -pedantic -ansi -Wall freopen.c 
    psycho@Shiva:~/Programming/Laboratory$ ./a.out 
    hello 
    psycho@Shiva:~/Programming/Laboratory$ cat stdout.log 
    This is stdout
    hello 
    psycho@Shiva:~/Programming/Laboratory$ cat stderr.log 
    This is stderr

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    23
    You could just close em... ...but that's ugly, if you wanna reuse them later.

  5. #5
    .
    Join Date
    Nov 2003
    Posts
    307
    If you opt for the dup(fd fd2 ) route consider dup2(fd fd1, fd fdnew ) as it's atomic- if it fails it doesn't close try to close fdnew (if it is in use) to create the file descriptor.

  6. #6
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    wtf are you talking about jim? Are you on crack?

  7. #7
    .
    Join Date
    Nov 2003
    Posts
    307
    In unix dup2 is an atomic (guaranteed not to be interrupted) call - this is generally a better choice than dup. ... see Rochkind 'Advanced Unix Programming' p 371.

    A lot of unix C programmers use freopen() to redirect, FWIW.

  8. #8
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    I see :]

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    I think in text mode a/a+, the file is created if it doesn't exist.

  10. #10
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    You are right dwks, a and a+ create the file if not already created.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Encapsulating cmd.exe
    By bennyandthejets in forum Windows Programming
    Replies: 4
    Last Post: 05-17-2005, 12:46 AM
  2. Can someone help me understand this example program
    By Guti14 in forum C Programming
    Replies: 6
    Last Post: 09-06-2004, 12:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21