1STDIO(3) Linux Programmer's Manual STDIO(3)
2
3
4
6 stdio - standard input/output library functions
7
9 #include <stdio.h>
10
11 FILE *stdin;
12 FILE *stdout;
13 FILE *stderr;
14
16 The standard I/O library provides a simple and efficient buffered
17 stream I/O interface. Input and output is mapped into logical data
18 streams and the physical I/O characteristics are concealed. The func‐
19 tions and macros are listed below; more information is available from
20 the individual man pages.
21
22 A stream is associated with an external file (which may be a physical
23 device) by opening a file, which may involve creating a new file. Cre‐
24 ating an existing file causes its former contents to be discarded. If
25 a file can support positioning requests (such as a disk file, as
26 opposed to a terminal), then a file position indicator associated with
27 the stream is positioned at the start of the file (byte zero), unless
28 the file is opened with append mode. If append mode is used, it is
29 unspecified whether the position indicator will be placed at the start
30 or the end of the file. The position indicator is maintained by subse‐
31 quent reads, writes and positioning requests. All input occurs as if
32 the characters were read by successive calls to the fgetc(3) function;
33 all output takes place as if all characters were written by successive
34 calls to the fputc(3) function.
35
36 A file is disassociated from a stream by closing the file. Output
37 streams are flushed (any unwritten buffer contents are transferred to
38 the host environment) before the stream is disassociated from the file.
39 The value of a pointer to a FILE object is indeterminate after a file
40 is closed (garbage).
41
42 A file may be subsequently reopened, by the same or another program
43 execution, and its contents reclaimed or modified (if it can be reposi‐
44 tioned at the start). If the main function returns to its original
45 caller, or the exit(3) function is called, all open files are closed
46 (hence all output streams are flushed) before program termination.
47 Other methods of program termination, such as abort(3) do not bother
48 about closing files properly.
49
50 At program startup, three text streams are predefined and need not be
51 opened explicitly: standard input (for reading conventional input),
52 standard output (for writing conventional output), and standard error
53 (for writing diagnostic output). These streams are abbreviated stdin,
54 stdout, and stderr. When opened, the standard error stream is not
55 fully buffered; the standard input and output streams are fully
56 buffered if and only if the streams do not refer to an interactive
57 device.
58
59 Output streams that refer to terminal devices are always line buffered
60 by default; pending output to such streams is written automatically
61 whenever an input stream that refers to a terminal device is read. In