James wrote:
>
> On Wed, 17 Feb 1999, David Rysdam wrote:
>
> # The second way is to close file descriptor 2, and re-open another
> # file/device with that number within your program.
>
> will C let you do that?
The answer to this question is almost invariably "yes".
>cos what'd happen if you closed stdout and
> forgot to re-open one and then did a printf().
#include<stdio.h>
#include<unistd.h>
int main(void)
{
close(0); //stdin
close(1); //stdout
close(2); //stderr
printf("hello world!\n");
return 0;
}
Try compiling and running this. Nothing happens. Most likely printf
returns an error about an invalid file descriptor, but I'm too lazy to
check it out.
>And you can't open random
> file descriptors (although if you just closed fd 2 a call to open() should
> use that...)
I don't know what you mean by random, but open() uses the lowest
available file descripter, so you are correct to say that after closing
2 the next opened file will be "stdout".