1fopen(3)                   Library Functions Manual                   fopen(3)
2
3
4

NAME

6       fopen, fdopen, freopen - stream open functions
7

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

12       #include <stdio.h>
13
14       FILE *fopen(const char *restrict pathname, const char *restrict mode);
15       FILE *fdopen(int fd, const char *mode);
16       FILE *freopen(const char *restrict pathname, const char *restrict mode,
17                     FILE *restrict stream);
18
19   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
20
21       fdopen():
22           _POSIX_C_SOURCE
23

DESCRIPTION

25       The fopen() function opens the file whose name is the string pointed to
26       by pathname and associates a stream with it.
27
28       The argument mode points to a string beginning with one of the  follow‐
29       ing sequences (possibly followed by additional characters, as described
30       below):
31
32       r      Open text file for reading.  The stream is positioned at the be‐
33              ginning of the file.
34
35       r+     Open  for  reading and writing.  The stream is positioned at the
36              beginning of the file.
37
38       w      Truncate file to zero length or create text  file  for  writing.
39              The stream is positioned at the beginning of the file.
40
41       w+     Open  for  reading  and writing.  The file is created if it does
42              not exist, otherwise it is truncated.  The stream is  positioned
43              at the beginning of the file.
44
45       a      Open  for  appending (writing at end of file).  The file is cre‐
46              ated if it does not exist.  The stream is positioned at the  end
47              of the file.
48
49       a+     Open  for  reading  and appending (writing at end of file).  The
50              file is created if it does not exist.  Output is always appended
51              to  the  end  of  the file.  POSIX is silent on what the initial
52              read position is when using this mode.  For glibc,  the  initial
53              file  position  for reading is at the beginning of the file, but
54              for Android/BSD/MacOS, the initial file position for reading  is
55              at the end of the file.
56
57       The  mode string can also include the letter 'b' either as a last char‐
58       acter or as a character between the characters in any of the  two-char‐
59       acter strings described above.  This is strictly for compatibility with
60       ISO C and has no effect; the 'b' is ignored  on  all  POSIX  conforming
61       systems,  including Linux.  (Other systems may treat text files and bi‐
62       nary files differently, and adding the 'b' may be a good idea if you do
63       I/O to a binary file and expect that your program may be ported to non-
64       UNIX environments.)
65
66       See NOTES below for details of glibc extensions for mode.
67
68       Any created file will have the mode S_IRUSR | S_IWUSR | S_IRGRP | S_IW‐
69       GRP  |  S_IROTH  |  S_IWOTH  (0666), as modified by the process's umask
70       value (see umask(2)).
71
72       Reads and writes may be intermixed on read/write streams in any  order.
73       Note  that  ANSI  C requires that a file positioning function intervene
7