16.1: Files and Streams

Since the beginning, we've been using ``standard input'' and ``standard output,'' two predefined I/O streams which are available to every C program. The disposition of these streams is left deliberately unclear: the program can assume that they're connected to the ``right place''; usually (for an interactive program) to the user's keyboard and screen, respectively. However, since a program typically doesn't know exactly where they go, it's possible to redirect them, behind the program's back, and thereby to apply a program to some noninteractive input or to capture its output, without rewriting the program or doing any special I/O programming. (This ability is a cornerstone of the Unix ``toolkit'' methodology. In Unix and several other systems, you can redirect the input or output of a program as you invoke it from the shell command line using the < or > characters.)

Standard input is assumed by functions like getchar, and standard output is assumed by functions like putchar and printf.

Of course, it's also possible to open files (or other I/O sources) explicitly. We can open files using the function fopen; certain systems may also provide specialized ways of opening streams connected to I/O devices or set up in more exotic ways. A successful call to fopen returns a pointer of type FILE *, that is, ``pointer to FILE,'' where FILE is a special type defined by <stdio.h>. A FILE * (also called ``file pointer'') is the handle by which we refer to an I/O stream in C. I/O functions which do not assume standard input or standard output all accept a FILE * argument telling them which stream to read from or write to. (Examples are getc, putc, and fprintf.) Notice that a file pointer which has been opened on a file is not the same thing as the file itself. A file pointer is a data structure which helps us access or manipulate the file.

Occasionally it is necessary to refer to the standard input or standard output in a situation which calls for a general-purpose FILE *. To handle these cases, there are two predefined constants: stdin and stdout. Both of these are of type FILE *, are declared in <stdio.h>, and can be used wherever a FILE * is required. (For example, we could simulate--or, in fact, implement--getchar as getc(stdin).)

There is also a third predefined stream, the ``standard error output.'' It has its own constant, stderr. By default, stderr is typically connected to the same output device as stdout; the difference between stdout and stderr is that stderr is not redirected when stdout is. stderr is, as its name implies, intended for error messages: if your program printed its error messages to stdout (e.g. by calling printf), they would disappear into the output file if the user redirected the standard output. Therefore, it's customary to print error messages (and also prompts, or anything else that shouldn't be redirected) to stderr, often by calling fprintf(stderr, message, ...).

Read sequentially: prev next up top

This page by Steve Summit // Copyright 1996-1999 // mail feedback