Previous | Table of Contents | Next |
Each standard header is described in its own subsection, as follows:
Header | Section | Header | Section |
---|---|---|---|
<stdio.h> | 3.10.1 | <stdarg.h> | 3.10.10 |
<string.h> | 3.10.2 | <stddef.h> | 3.10.11 |
<ctype.h> | 3.10.3 | <assert.h> | 3.10.12 |
<stdlib.h> | 3.10.4 | <errno.h> | 3.10.13 |
<math.h> | 3.10.5 | <limits.h> | 3.10.14 |
<time.h> | 3.10.6 | <float.h> | 3.10.15 |
<signal.h> | 3.10.7 | <iso646.h> | 3.10.16 |
<setjmp.h> | 3.10.8 | <wchar.h> | 3.10.17 |
<locale.h> | 3.10.9 | <wctype.h> | 3.10.18 |
Cs standard I/O library defines a set of functions, all declared in the header <stdio.h> , for performing efficient, buffered, portable I/O to files (and perhaps other devices), and also for performing a few filesystem-related operations. Most of the functions in the stdio library revolve around the concept of a stream, which is a sequence of bytes read from or written to some file or external device. Streams are manipulated by pointers of type FILE *, where FILE is an opaque type defined in <stdio.h>. The FILE * value (also known as a file pointer, handle, or descriptor) represents the state of a stream and is used in all operations on that stream. Streams are generally opened with the fopen function, but there are also three constant, predefined, preopened streams, stdin, stdout, and stderr, which are respectively the standard (or default) input, output, and error streams. These are typically connected to the users keyboard and screen by default, but may also be redirected in various ways. (These streams are constant in that their identifiers cannot portably be reassigned, although they may be redirected using freopen. The standard error stream is intended to be used for error messages that should not be redirected along with the standard output.)
We have already met a few I/O functions that operated implicitly on the standard input or standard output. For example, getchar reads characters from stdin, and printf prints to stdout. For each function that implicitly uses stdin or stdout, there is a corresponding function that permits an input or output stream to be explicitly specified.
In addition to character I/O and formatted output, the stdio library also supports reading and writing individual lines of text, I/O with arbitrary blocks of text, a form of formatted input, and several other operations. The header defines several types and constants, including the FILE type and the constant EOF, which is returned by several functions to indicate end-of-file or error conditions.
3.10.1.1. Opening and Closing Streams
fopen
FILE *fopen(const char *name, const char *mode)
fopen attempts to open the file name. If successful, it returns an open stream; if not, it returns a null pointer. The mode is a string indicating how the file should be opened. The first character of the string must be r, w, or a, indicating that the file is to be opened for reading, writing, or appending, respectively. Additional characters in the mode string request optional behavior: A + character requests that the file be opened with the ability to both read and write, and a b character indicates that the file should be treated as binary, as opposed to the default text. (Text file processing implies that Cs newline character, \n, is translated to and from the underlying operating systems end-of-line representation. On MS-DOS-based systems, text processing also implies that the character with value 26, control-Z or ASCII SUB, is treated as an in-band end-of-file character. Binary mode suppresses these translations.) Any additional characters in the mode string are typically ignored but may be defined as extensions in certain environments.
freopen
FILE *freopen(const char *name, const char *mode, FILE *fp)
freopen is much like fopen, except that instead of constructing a new FILE object, it reuses the existing one pointed to by fp (which is first closed, as if by calling fclose, if it was already in use). freopen is typically used to redirect one of the constant streams stdin, stdout, or stderr from within a program. For example, after calling
freopen(output.log, w, stdout);
all future output generated by putchar and printf (and all other operations referring to stdout) will be performed on the file output.log.
fclose
int fclose(FILE *fp)
An open stream fp is closed, causing any remaining buffered output to be written out and any resources to be reclaimed, by calling fclose. The return value is EOF if there are any errors (e.g., while writing buffered output), and 0 otherwise. An fclose of all open streams is automatically performed on normal exit from a program.
fflush
int fflush(FILE *fp)
For an output stream fp, fflush causes any buffered data to be immediately written out. The return value is 0, or EOF if an error occurs (e.g., while writing the buffered data).
Previous | Table of Contents | Next |