why does this work?
hiho@ll
excuse for the generalized subject, but i was to uncreative to post it in another way well my "problem": i have this code: Code:
#include <stdio.h> Code:
g++ -o descr.o descr.test.c in the prog itself i only close stderr (2) and write the String TEST to stdout (1) the commandline redirects stderr to a file err.out and stdout to stderr what i'm asking myself now is why does this work? if i do a cat err.out i get TEST this means that printf("TEST"); has been written to err.out through stderr, which i closed at the begining of the prog!? thx@ll |
Hi,
I believe it works because of the following: 1) Shell redirects stderr to err.out 2) Shell closes generic stdout file descriptor, dups current stderr file descriptor and assigns it to stdout Now when your program begins you still have two different file descriptors, one for stdout and one for stderr and they are both pointing to err.out 3) Your program closes file descriptor for stderr, however stdout file descriptor is still fully functional 4) You program writes text to stdout, since it was going to file err.out the text is placed into the file. The points to note are that the shell does not point stdout to stderr's file descriptor, it duplicates it thus creating a valid and autonomous new file descriptor for stdout. When you close stderr, you are only closing one instance of the access to your err.out file. I hope that my explanation is clear enough. |
sounds very good!
thx |
All times are GMT -5. The time now is 04:55 PM. |