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