stdio.h

The header file contains prototypes for functions that do stream I/O. That is they treat I/O devices as reading or writing a stream of bytes. This header file is the most commonly used one, as it lets the program communicate with the external world. These functions are:

    clearerr()     flushall()    fwrite()     scanf()
    fclose()       fopen()       getc()       setbuf()
    fcloseall()    fprintf()     getchar()    setvbuf()
    fdopen()       fputc()       gets()       sprintf()
    feof()         fputchar()    getw()       sscanf()
    ferror()       fputs()       printf()     tempnam()
    fflush()       fread()       putc()       tmpfile()
    fgetc()        freopen()     putchar()    tmpnam()
    fgetchar()     fscanf()      puts()       ungetc()
    fgetpos()      fseek()       putw()       vfprintf()
    fgets()        fsetpos()     rewind()     vprintf()
    fileno()       ftell()       rmtmp()      vsprintf()

This extensive list of functions belong to several categories. They take care of error conditions and test for end of file. They can open files and close files. A file cannot be used unless it is opened. But there are always at least three streams open:

        stdin        an input (usually from the keyboard)
        stdout       an output (usually to the screen)
        stderr       an error output (usually to the screen)

A stream can be opened in different mode for:

        read
        write
        append
        combinations of the above

According to the mode in which a file is opened for write, the length after a write will be either just after the current write, or the maximum of the original length and just after the write.

Once a stream is open one can read or write to it (if the mode is such). The reading or writing can be either: one character at a time, a specified number of characters at a time, one "int" at a time, one string (on read up until the next newline), or it can be formatted.

Formatted I/O is still a stream of bytes but the bytes are a sequence of characters which express a number (integer or fractional) or a string, where the length, precision, and alignment can all be specified. This same formatted conversion between a string of bytes and data values can take place either from or to an I/O device or another string (see for example: printf() vs. sprintf()).

There are also functions to determine the position in the file or to change the position in the file - affecting what will be read next or where the next write will go.

Beware that most of these functions have internal state and do not behave in the simple way one might expect. As such the action of one function will affect what another will do when called.

Stream I/O is actually buffered for speed when this makes sense, especially from or to disks and tapes. This means that even though you read or write one byte at a time, the operating system will only read or write sectors or blocks or tracks at a time. This saves a lot of time.

But, it does have a side effect that a write done is not really done until the operating system actually write the internally buffered data to the physical media. This is why there are functions to flush the output. One can also flush input. This causes all buffered but unrequested input to be discarded. The behavior of this buffering varies from operating system to operating system.


© 1991-2008 Prem Sobel. All Rights Reserved.