If you need to read from or write to a file from within a C program, you must
use a ``file pointer,'' which tells C where to read or write. Just as C
defines integer constants (for example 1, 5, and -2) and floating point
constants (for example 3.5 and -9.2), C also predefines several file pointers
(in ``stdio.h''). The two of immediate interest are called stdin
(standard input) and stdout (standard output). You should think of
stdin as ``pointing'' to the keyboard, and stdout as ``pointing'' to the
display.
Look at io2.c. How does it differ
from the previous program?
Click here for the answer
Despite the surface differences, if you run the program you will find that it
behaves exactly the same as the first one you looked at. Here's the idea.
fscanf behaves just like scanf, except that a file pointer must be
given as the first argument to tell it where to read. Similarly, fprintf
behaves just like printf, except that a file pointer must be given as the
first argument to tell it where to write.
As it turns out, stdin is exactly the place from which scanf reads
(the keyboard), and stdout is exactly the place to which printf
writes (the display). So if you use stdin as its first argument,
fscanf is identical to scanf; if you use stdout as its first
argument, fprintf is identical to printf.
Joseph L. Zachary
Hamlet Project
Department of Computer Science
University of Utah