|
Input/Output <stdio.h> |
Top Previous Next |
|
The standard I/O functions provide a portable I/O interface for C programs. They are available in the form described here in most implementations of C. They also provide buffering between user programs and files or devices. This means that I/O transfers to or from real files remain efficient even if data is transferred between the file and the user program in small units (e.g. one character at a time). On output, user data is placed in a buffer allocated 'behind the scenes' by the standard I/O functions, until the buffer becomes full, at which point the contents of the buffer are written en masse to the file. This technique achieves a speed-up because disk devices are optimized for block transfers. The situation for input is similar.
Other standard I/O functions allow random file access and conversion of numeric data between internal (binary) and external (character string) representations.
Before you can read or write the data in a file, the fopen function must be called to create a connection to the file. The name of the file is passed to fopen, which, if the file is accessible, returns a pointer to a structure of type FILE. The calling program must use this file pointer to refer to the file in subsequent I/O operations (fputc, for example, requires a file pointer argument to identify the file which is to be written). The FILE type is declared in stdio.h.
After performing I/O on an open file, the connection with the file may be broken by closing the file with fclose; you should try to close files when they are no longer in use, since some implementations place a limit on the number of files that may be open at once. You can open files again after they have been closed. Closing all files explicitly at the end of a program is, however, unnecessary; this is done automatically by the standard I/O system when a task's main function returns or when the exit function is called.
In the following example, a file named fred is opened, some ASCII characters are written out to it and the file is closed. For clarity, no error checking is performed in the example.
#include <stdio.h> // standard I/O declarations
main() { FILE *fp; // file pointer variable fp=fopen("fred", // file name "w"); // open for writing fprintf( // formatted output routine fp, // file pointer (identifies file) "Hi!\n" // text string to be written ); fclose(fp); // disconnect file }
To simplify writing programs that read one sequential input file, process it and write another sequential output file, most implementations of C provide some means external to a program (e.g. the User Interface) to connect at run time files or devices other than the default to the standard input and output of a program. This means that programs may be written and tested using the keyboard and screen for standard input and output, then run unchanged using files, without the program itself needing to open the files by name. You can find a description of the mechanism the server uses to redefine the standard I/O streams here. |