1FDOPEN(3P) POSIX Programmer's Manual FDOPEN(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 fdopen — associate a stream with a file descriptor
13
15 #include <stdio.h>
16
17 FILE *fdopen(int fildes, const char *mode);
18
20 The fdopen() function shall associate a stream with a file descriptor.
21
22 The mode argument is a character string having one of the following
23 values:
24
25 r or rb Open a file for reading.
26
27 w or wb Open a file for writing.
28
29 a or ab Open a file for writing at end-of-file.
30
31 r+ or rb+ or r+b
32 Open a file for update (reading and writing).
33
34 w+ or wb+ or w+b
35 Open a file for update (reading and writing).
36
37 a+ or ab+ or a+b
38 Open a file for update (reading and writing) at end-of-
39 file.
40
41 The meaning of these flags is exactly as specified in fopen(), except
42 that modes beginning with w shall not cause truncation of the file.
43
44 Additional values for the mode argument may be supported by an imple‐
45 mentation.
46
47 The application shall ensure that the mode of the stream as expressed
48 by the mode argument is allowed by the file access mode of the open
49 file description to which fildes refers. The file position indicator
50 associated with the new stream is set to the position indicated by the
51 file offset associated with the file descriptor.
52
53 The error and end-of-file indicators for the stream shall be cleared.
54 The fdopen() function may cause the last data access timestamp of the
55 underlying file to be marked for update.
56
57 If fildes refers to a shared memory object, the result of the fdopen()
58 function is unspecified.
59
60 If fildes refers to a typed memory object, the result of the fdopen()
61 function is unspecified.
62
63 The fdopen() function shall preserve the offset maximum previously set
64 for the open file description corresponding to fildes.
65
67 Upon successful completion, fdopen() shall return a pointer to a
68 stream; otherwise, a null pointer shall be returned and errno set to
69 indicate the error.
70
72 The fdopen() function shall fail if:
73
74 EMFILE {STREAM_MAX} streams are currently open in the calling process.
75
76 The fdopen() function may fail if:
77
78 EBADF The fildes argument is not a valid file descriptor.
79
80 EINVAL The mode argument is not a valid mode.
81
82 EMFILE {FOPEN_MAX} streams are currently open in the calling process.
83
84 ENOMEM Insufficient space to allocate a buffer.
85
86 The following sections are informative.
87
89 None.
90
92 File descriptors are obtained from calls like open(), dup(), creat(),
93 or pipe(), which open files but do not return streams.
94
96 The file descriptor may have been obtained from open(), creat(),
97 pipe(), dup(), fcntl(), or socket(); inherited through fork(),
98 posix_spawn(), or exec; or perhaps obtained by other means.
99
100 The meanings of the mode arguments of fdopen() and fopen() differ. With
101 fdopen(), open for write (w or w+) does not truncate, and append (a or
102 a+) cannot create for writing. The mode argument formats that include a
103 b are allowed for consistency with the ISO C standard function fopen().
104 The b has no effect on the resulting stream. Although not explicitly
105 required by this volume of POSIX.1‐2017, a good implementation of
106 append (a) mode would cause the O_APPEND flag to be set.
107
109 None.
110
112 Section 2.5.1, Interaction of File Descriptors and Standard I/O
113 Streams, fclose(), fmemopen(), fopen(), open(), open_memstream(),
114 posix_spawn(), socket()
115
116 The Base Definitions volume of POSIX.1‐2017, <stdio.h>
117
119 Portions of this text are reprinted and reproduced in electronic form
120 from IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
121 table Operating System Interface (POSIX), The Open Group Base Specifi‐
122 cations Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of
123 Electrical and Electronics Engineers, Inc and The Open Group. In the
124 event of any discrepancy between this version and the original IEEE and
125 The Open Group Standard, the original IEEE and The Open Group Standard
126 is the referee document. The original Standard can be obtained online
127 at http://www.opengroup.org/unix/online.html .
128
129 Any typographical or formatting errors that appear in this page are
130 most likely to have been introduced during the conversion of the source
131 files to man page format. To report such errors, see https://www.ker‐
132 nel.org/doc/man-pages/reporting_bugs.html .
133
134
135
136IEEE/The Open Group 2017 FDOPEN(3P)