9.10. Input and output9.10.1. IntroductionOne of the reasons that has prevented many programming languages from becoming widely used for ‘real programming’ is their poor support for I/O, a subject which has never seemed to excite language designers. C has avoided this problem, oddly enough, by having no I/O at all! The C language approach has always been to do I/O using library functions, which ensures that system designers can provide tailored I/O instead of being forced to change the language itself. As C has evolved, a library package known as the ‘Standard I/O Library’ or stdio, has evolved with it and has proved to be both flexible and portable. This package has now become part of the Standard. The old stdio package relied heavily on the UNIX model of file access, in particular the assumption that there is no distinction between unstructured binary files and files containing readable text. Many operating systems do maintain a distinction between the two, and to ensure that C programs can be written portably to run on both types of file model, the stdio package has been modified. There are changes in this area which affect many existing programs, although strenuous efforts were taken to limit the amount of damage. Old C programs should still be able work unmodified in a UNIX environment. 9.10.2. The I/O modelThe I/O model does not distinguish between the types of physical devices supporting the I/O. Each source or sink of data (file) is treated in the same way, and is viewed as a stream of bytes. Since the smallest object that can be represented in C is the character, access to a file is permitted at any character boundary. Any number of characters can be read or written from a movable point, known as the file position indicator. The characters will be read, or written, in sequence from this point, and the position indicator moved accordingly. The position indicator is initially set to the beginning of a file when it is opened, but can also be moved by means of positioning requests. (Where random access is not possible, the file position indicator is ignored.) Opening a file in append mode has an implementation defined effect on the stream's file position indicator. The overall effect is to provide sequential reads or writes unless the stream was opened in append mode, or the file position indicator is explicitly moved. There are two types of file, text files and binary files, which, within a program, are manipulated as text streams and binary streams once they have been opened for I/O. The stdio package does not permit operations on the contents of files ‘directly’, but only by viewing them as streams. 9.10.2.1. Text streamsThe Standard specifies what is meant by the term text stream,
which essentially considers a file to contain lines of text. A line is
a sequence of zero or more characters terminated by a newline character.
It is quite possible that the actual representation of lines in the
external environment is different from this and there may be
transformations of the data stream on the way in and out of the program;
a common requirement is to translate the ‘ Data read in from a text stream is guaranteed to compare equal to the data that was earlier written out to the file if the data consists only of complete lines of printable characters and the control characters horizontal-tab and newline, no newline character is immediately preceded by space characters and the last character is a newline. It is guaranteed that, if the last character written to a text file is a newline, it will read back as the same. It is implementation defined whether the last line written to a text file must terminate with a newline character; this is because on some implementations text files and binary files are the same. Some implementations may strip the leading space from lines consisting only of a space followed by a newline, or strip trailing spaces at the end of a line! An implementation must support text files with lines containing at least 254 characters, including the terminating newline. Opening a text stream in update mode may result in a binary stream in some implementations. Writing on a text stream may cause some implementations to truncate the file at that point—any data beyond the last byte of the current write being discarded. 9.10.2.2. Binary streamsA binary stream is a sequence of characters that can be used to record
a program's internal data, such as the contents of structures or arrays in
binary form. Data read in from a binary stream will always compare equal
to data written out earlier to the same stream, under the same
implementation. In some circumstances, an implementation-defined number
of The contents of binary files are exceedingly machine specific, and not, in general, portable. 9.10.2.3. Other streamsOther stream types may exist, but are implementation defined. 9.10.3. The stdio.h header fileTo provide support for streams of the various kinds, a number of
functions and macros exist. The
9.10.4. Opening, closing and buffering of streams9.10.4.1. OpeningA stream is connected to a file by means of the Three streams are available without any special action; they are
normally all connected to the physical device associated with the
executing program: usually your terminal. They are referred to by the
names As mentioned earlier, the file position indicator may or may not be movable, depending on the underlying device. It is not possible, for example, to move the file position indicator on stdin if that is connected to a terminal, as it usually is. All non-temporary files must have a filename, which is a string. The rules for what constitutes valid filenames are implementation defined. Whether a file can be simultaneously open multiple times is also implementation defined. Opening a new file may involve creating the file. Creating an existing file causes its previous contents to be discarded. 9.10.4.2. ClosingFiles are closed by explicitly calling 9.10.4.3. BufferingThere are three types of buffering:
The buffering associated with a stream can always be flushed by using
9.10.5. Direct file manipulationA number of functions exist to operate on files directly. #include <stdio.h> int remove(const char *filename); int rename(const char *old, const char *new); char *tmpnam(char *s); FILE *tmpfile(void);
9.10.6. Opening named filesNamed files are opened by a call to the #include <stdio.h> FILE *fopen(const char *pathname, const char *mode); The Files can be opened in a variety of modes, such as read mode for reading data, write mode for writing data, and so on. Note that if you only want to write data to a file, The Standard list of modes is shown in Table 9.3, although implementations may permit extra modes by appending extra characters at the end of the modes.
Beware that some implementations of binary files may pad the last record
with If a file is opened in append mode, all writes will occur at the
end of the file, regardless of attempts to move the file position indicator
with Attempts to open a file in read mode, indicated by an ' Files opened for update (‘ It may also be possible in some implementations to omit the
Streams opened by fopen are fully buffered only if they are not connected to an interactive device; this ensures that prompts and responses are handled properly. If 9.10.7. FreopenThe
#include <stdio.h>
FILE *freopen(const char *pathname,
const char *mode, FILE *stream);
The 9.10.8. Closing filesAn open file is closed using #include <stdio.h> int fclose(FILE *stream); Any unwritten data buffered for Zero is returned on success, 9.10.9. Setbuf, setvbufThese two functions are used to change the buffering strategy for an open stream:
#include <stdio.h>
int setvbuf(FILE *stream, char *buf,
int type, size_t size);
void setbuf(FILE *stream, char *buf);
They must be used before the file is either read from or
written to. The
The A call of No value is returned by 9.10.10. Fflush#include <stdio.h> int fflush(FILE *stream); If The most recent operation on the stream must have been an output operation; if not, the behaviour is undefined. A call of
|
The C BookThis book is published as a matter of historical interest. Please read the copyright and disclaimer information. GBdirect Ltd provides up-to-date training and consultancy in C, Embedded C, C++ and a wide range of other subjects based on open standards if you happen to be interested. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
West Yorkshire Office
GBdirect Ltd
Training: 0800 651 0338 Please call between 0900 and 1700 (UK time) on Monday to Friday South East Regional Office
GBdirect Ltd
Training: 0800 651 0338 Please call between 0900 and 1700 (UK time) on Monday to Friday Please note: |