The ``Standard I/O Library''
is a collection of routines
The standard I/O library is available on each system that supports C,
their system interactions
can be transported from one system to another essentially without change.
In this section, we will discuss the basics of the standard I/O library.
The appendix contains a more complete description of its capabilities.
The programs written so far have all
read the standard input and written the standard output,
which we have assumed are magically pre-defined.
is to write a program that accesses
already connected to the program.
which counts the lines, words and characters
For instance, the command
prints the number of lines, words and characters
The question is how to arrange for the named files
that is, how to connect the file system names
to the I/O statements which actually read the data.
Before it can be read or written
by the standard library function
does some housekeeping and negotiation with the operating system,
and returns an internal name
which must be used in subsequent
reads or writes of the file.
This internal name is actually a pointer,
which contains information about the file,
such as the location of a buffer,
the current character position in the buffer,
whether the file is being read or written,
Users don't need to know the details,
because part of the standard I/O definitions
is a structure definition called
The only declaration needed for a file pointer
The second argument is the
also as a character string,
which indicates how you intend to
The only allowable modes are
If a file that you open for writing or appending does not exist,
Opening an existing file for writing causes the old contents
Trying to read a file that does not exist
and there may be other causes of error
(like trying to read a file
when you don't have permission).
will return the null pointer
(which is defined as zero in
The next thing needed is a way to read or write the file
There are several possibilities,
returns the next character from a file;
it needs the file pointer to tell it what file.
the next character from the file referred to by
when it reaches end of file.
When a program is started, three files are opened automatically,
and file pointers are provided for them.
These files are the standard input,
and the standard error output;
the corresponding file pointers are
Normally these are all connected to the terminal,
may be redirected to files or pipes as described in
are pre-defined in the I/O library
as the standard input, output and error files;
they may be used anywhere an object of type
so don't try to assign to them.
With some of the preliminaries out of the way,
is one that has been found
convenient for many programs:
if there are command-line arguments, they are processed in order.
If there are no arguments, the standard input
This way the program can be used stand-alone
or as part of a larger process.
main(argc, argv) /* wc: count lines, words, chars */
long linect, wordct, charct;
long tlinect = 0, twordct = 0, tcharct = 0;
if (argc > 1 && (fp=fopen(argv[i], "r")) == NULL) {
fprintf(stderr, "wc: can't open %s\n", argv[i]);
linect = wordct = charct = inword = 0;
while ((c = getc(fp)) != EOF) {
if (c == ' ' || c == '\t' || c == '\n')
printf("%7ld %7ld %7ld", linect, wordct, charct);
printf(argc > 1 ? " %s\n" : "\n", argv[i]);
printf("%7ld %7ld %7ld total\n", tlinect, twordct, tcharct);
save that the first argument is a file pointer
that specifies the file to be
it breaks the connection between the file pointer and the external name
file pointer for another file.
Since there is a limit on the number
that a program may have open simultaneously,
it's a good idea to free things when they are no longer needed.
There is also another reason to call
\(em it flushes the buffer
is called automatically for each open file
when a program terminates normally.)
Error Handling \(em Stderr and Exit
is assigned to a program in the same way that
appears on the user's terminal
even if the standard output is redirected.
writes its diagnostics on
so that if one of the files can't
be accessed for some reason,
finds its way to the user's terminal instead of disappearing
The program actually signals errors in another way,
to terminate program execution.
is available to whatever process
called it (see Section 6),
so the success or failure
of the program can be tested by another program
that uses this one as a sub-process.
By convention, a return value of 0
signals that all is well;
non-zero values signal abnormal situations.
for each open output file,
to flush out any buffered output,
causes immediate termination without any buffer flushing;
it may be called directly if desired.
Miscellaneous I/O Functions
The standard I/O library provides several other I/O functions
besides those we have illustrated above.
etc., is buffered (except to
to force it out immediately, use
except that its first argument is a file pointer
that specifies the file from which the input comes;
except that the first argument names a character string
instead of a file pointer.
The conversion is done from the string
.UL fgets(buf,\ size,\ fp)
copies the next line from
up to and including a newline,
``pushes back'' the character
Only one character of pushback per file is permitted.