/* To be thorough, I would add a close here. */
It sounds more like work creation than being thorough.
if( fclose( stdin ))
/* handle_error */;
Handle error? If it ain't broken, don't fix it!
in = fopen(argv[1], "r");
See addendum above. No point in leaving unused
resources open.
But why close a resource that gets closed on
program termination anyway? Why preclude the
possibility of using stdin in the cases where
a file name is supplied? Note there is no
portable way of restoring console input if you
close stdin.
The main reason is to catch the coding error:
in_file = stdin;
....
fread( stdin ...) /* oops, should have read from in_file ! */
Also, there are many times when a program bumps into the
upper limit on available file descriptors. Granted, having
only 1 more will probably not avoid the problem, but
on a system which only gives you 16, one is
pretty substantial.
Clearly, a program that wants to take input from
stdin and a file shouldn't close stdin, but it
is a mistake to refer to stdin as "console
input". In the following, ain't no console
input to any of the programs:
$ < foo grep pattern | grep -v foo | sed 's/foo/bar/g' | wc -l
stdin is NOT the keyboard, and stdout is NOT the terminal.
Often, a tty is attached to the std I/O streams, but it
is a mistake to always think of them that way, and leads
to many programmers doing stupid things like corrupting the
output stream with stupid messages. Eg:
printf( "working: processed %d buffers so far\r", buf_count );
Somehow, that programmer has the notion that stdout is
a terminal rather than the standard place to write
output.