Add Book to My BookshelfPurchase This Book Online

Chapter 4 - The Standard I/O Library

UNIX Systems Programming for SVR4
David A. Curry
 Copyright © 1996 O'Reilly & Associates, Inc.

Opening and Closing Files
Before any data can be read from or written to a file, that file must be opened for reading or writing (or both). Opening a file causes the operating system to locate (or create) the file on the disk, allocate an entry in the process' open file table, and set up assorted internal structures for moving data between the file and your program. In the case of the Standard I/O Library, opening a file also allocates buffers internal to the library that are used to move data between your program and the file in an efficient manner. The Standard I/O Library function for opening a file is called fopen:
    #include <stdio.h>
    FILE *fopen(const char *filename, const char *type);
The character string filename contains the pathname of the file to be opened, and the type character string describes the type of stream that is to be created. type can have any of the following values:
r
Open the file for reading only. The file must already exist.
w
Open the file for writing only. If the file does not exist, it is created. If the file does exist, it is truncated to zero length (any data already in the file will be lost).
a
Open the file for writing (appending). If the file does not exist, it is created. If the file does exist, all writes to the file are appended to the end (any data already in the file will not be lost).
r+
Open the file for both reading and writing. The file must already exist.
w+
Open the file for both reading and writing. If the file does not exist, it is created. If the file does exist, it is truncated to zero length.
a+
Open the file for both reading and writing (appending). If the file does not exist, it is created. If the file does exist, all writes to the file are appended to the end.
All type strings can also have a b contained in them, as in rb, w+b, or ab+. The b informs the library routines that the file is a “binary” file (as opposed to a text file), which is necessary on some operating systems. Because UNIX does not distinguish between binary and text files, the b is simply ignored.
If the file can be opened successfully, a file pointer to the open stream is returned. If the file cannot be opened, the constant NULL is returned and an error code is placed in the external variable errno.
Once a program is finished with a file, it should close the file. This causes any buffered writes to be flushed to the disk, frees up memory in the library associated with the file's buffering, and frees up the operating system resources (such as buffers and file table entry) used by that file. The Standard I/O Library function to close a file is called fclose:
    #include <stdio.h>
    int fclose(FILE *stream);
If the file referenced by stream is closed successfully, fclose returns zero. If the close fails, the constant EOF is returned and an error code is placed in the external variable errno.
Porting Notes
As mentioned earlier, the Standard I/O Library has been around for a long time, and there aren't too many significant differences between versions. The b character in the type argument was first introduced in XENIX and may not be understood by older versions of the library. However, it is a part of the ANSI C standard, and so most newer versions should support it. To be safe, though, always place the + after the first type character, followed by the b.
Some very old versions of the library may not understand the + notation, but as this should not be of concern on any modern system, don't worry about portability when using it.

Previous SectionNext Section
Books24x7.com, Inc © 2000 –  Feedback