1FOPEN(3) Linux Programmer's Manual FOPEN(3)
2
3
4
6 fopen, fdopen, freopen - stream open functions
7
9 #include <stdio.h>
10
11 FILE *fopen(const char *path, const char *mode);
12
13 FILE *fdopen(int fd, const char *mode);
14
15 FILE *freopen(const char *path, const char *mode, FILE *stream);
16
17 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
18
19 fdopen(): _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _POSIX_SOURCE
20
22 The fopen() function opens the file whose name is the string pointed to
23 by path and associates a stream with it.
24
25 The argument mode points to a string beginning with one of the follow‐
26 ing sequences (Additional characters may follow these sequences.):
27
28 r Open text file for reading. The stream is positioned at the
29 beginning of the file.
30
31 r+ Open for reading and writing. The stream is positioned at the
32 beginning of the file.
33
34 w Truncate file to zero length or create text file for writing.
35 The stream is positioned at the beginning of the file.
36
37 w+ Open for reading and writing. The file is created if it does
38 not exist, otherwise it is truncated. The stream is positioned
39 at the beginning of the file.
40
41 a Open for appending (writing at end of file). The file is cre‐
42 ated if it does not exist. The stream is positioned at the end
43 of the file.
44
45 a+ Open for reading and appending (writing at end of file). The
46 file is created if it does not exist. The initial file position
47 for reading is at the beginning of the file, but output is
48 always appended to the end of the file.
49
50 The mode string can also include the letter 'b' either as a last char‐
51 acter or as a character between the characters in any of the two-char‐
52 acter strings described above. This is strictly for compatibility with
53 C89 and has no effect; the 'b' is ignored on all POSIX conforming sys‐
54 tems, including Linux. (Other systems may treat text files and binary
55 files differently, and adding the 'b' may be a good idea if you do I/O
56 to a binary file and expect that your program may be ported to non-Unix
57 environments.)
58
59 See NOTES below for details of glibc extensions for mode.
60
61 Any created files will have mode S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP
62 | S_IROTH | S_IWOTH (0666), as modified by the process's umask value
63 (see umask(2)).
64
65 Reads and writes may be intermixed on read/write streams in any order.
66 Note that ANSI C requires that a file positioning function intervene
67 between output and input, unless an input operation encounters end-of-
68 file. (If this condition is not met, then a read is allowed to return
69 the result of writes other than the most recent.) Therefore it is good
70 practice (and indeed sometimes necessary under Linux) to put an
71 fseek(3) or fgetpos(3) o