DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
C compilation system

Named files

Any file other than standard input, standard output, and standard error must be explicitly opened before your program can read from or write to the file. You open a file with the standard library function fopen(). fopen() takes a pathname, asks the system to keep track of the connection between your program and the file, and returns a pointer that you can then use in functions that perform other I/O operations.

The pointer is to a structure of type FILE, defined in stdio.h, that contains information about the file including the location of its buffer, and the current character position in the buffer. Your program must have a declaration such as:

   FILE *fin;
which says that fin is a pointer to a FILE. The statement:
   fin = fopen("filename", "r");
associates a structure of type FILE with filename, the pathname of the file to open, and returns a pointer to it. The "r" means that the file is to be opened for reading. This argument is known as the mode. There are modes for reading, writing, and both reading and writing.

In practice, the file open function is often included in an if statement:

   if ((fin = fopen("filename", "r")) == NULL)
       (void)fprintf(stderr,"Cannot open input file %s\n",
           "filename");
which uses fopen() to return a NULL pointer if it cannot open the file. To avoid falling into the immediately following code on failure, you can call exit(), which causes your program to quit:
   if ((fin = fopen("filename", "r")) == NULL) {
       (void)fprintf(stderr,"Cannot open input file %s\n",
           "filename");
       exit(1);
   }
Once you have opened the file, use the pointer fin in functions or macros to refer to the stream associated with the opened file:
   int c;
   c = getc(fin);
brings in one character from the stream into an integer variable called c. The variable c is declared as an integer even though it is reading characters because getc() returns an integer. Getting a character is often incorporated in some flow-of-control mechanism such as:
   while ((c = getc(fin)) != EOF)
        .
        .
        .
that reads through the file until EOF is returned. EOF, NULL, and the macro getc() are all defined in stdio.h. getc() and other macros in the standard I/O package keep advancing a pointer through the buffer associated with the stream. The UNIX operating system and the standard I/O functions are responsible for seeing that the buffer is refilled if you are reading the file, or written to the output file if you are producing output, when the pointer reaches the end of the buffer.

Your program may have multiple files open simultaneously, 20 or more depending on system configuration. If your program needs to open more files than it is permitted to have open simultaneously, you can use the standard library function fclose() to free up one of the structures of type FILE by breaking the connection between your pointer to the structure and the pathname of the file your program has opened. You can now make another call to fopen(). For output files, an fclose() call makes sure that all output has been sent from the output buffer before disconnecting the file. exit() closes all open files for you, but it also gets you completely out of your process, so you should use it only when you are sure you are finished.


Next topic: Passing command line arguments
Previous topic: Standard files

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003