1 .\" Copyright 2005, 2012, 2016 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" %%%LICENSE_START(GPL_NOVERSION_ONELINE)
4 .\" Distributed under the GPL.
7 .TH FMEMOPEN 3 2021-03-22 "GNU" "Linux Programmer's Manual"
9 fmemopen \- open memory as stream
14 .BI "FILE *fmemopen(void *"buf ", size_t "size ", const char *" mode ");"
18 Feature Test Macro Requirements for glibc (see
19 .BR feature_test_macros (7)):
25 _POSIX_C_SOURCE >= 200809L
32 function opens a stream that permits the access specified by
34 The stream allows I/O to be performed on the string or memory buffer
40 argument specifies the semantics of I/O on the stream,
41 and is one of the following:
44 The stream is opened for reading.
47 The stream is opened for writing.
50 Append; open the stream for writing,
51 with the initial buffer position set to the first null byte.
54 Open the stream for reading and writing.
57 Open the stream for reading and writing.
58 The buffer contents are truncated
59 (i.e., \(aq\e0\(aq is placed in the first byte of the buffer).
62 Append; open the stream for reading and writing,
63 with the initial buffer position set to the first null byte.
65 The stream maintains the notion of a current position,
66 the location where the next I/O operation will be performed.
67 The current position is implicitly updated by I/O operations.
68 It can be explicitly updated using
72 In all modes other than append,
73 the initial position is set to the start of the buffer.
74 In append mode, if no null byte is found within the buffer,
75 then the initial position is
80 is specified as NULL, then
85 This is useful for an application that wants to write data to
86 a temporary buffer and then read it back again.
87 The initial position is set to the start of the buffer.
88 The buffer is automatically freed when the stream is closed.
89 Note that the caller has no way to obtain a pointer to the
90 temporary buffer allocated by this call (but see
91 .BR open_memstream (3)).
95 is not NULL, then it should point to a buffer of at least
97 bytes allocated by the caller.
99 When a stream that has been opened for writing is flushed
103 a null byte is written at the end of the buffer if there is space.
104 The caller should ensure that an extra byte is available in the
111 In a stream opened for reading,
112 null bytes (\(aq\e0\(aq) in the buffer do not cause read
113 operations to return an end-of-file indication.
114 A read from the buffer will indicate end-of-file
115 only when the current buffer position advances
117 bytes past the start of the buffer.
119 Write operations take place either at the current position
120 (for modes other than append), or at the current size of the stream
123 Attempts to write more than
125 bytes to the buffer result in an error.
126 By default, such errors will be visible
127 (by the absence of data) only when the
130 Disabling buffering with the following call
131 may be useful to detect errors at the time of an output operation:
133 setbuf(stream, NULL);
135 Upon successful completion,
140 Otherwise, NULL is returned and
142 is set to indicate the error.
145 was already available in glibc 1.0.x.
147 For an explanation of the terms used in this section, see
155 Interface Attribute Value
158 T} Thread safety MT-Safe
165 This function is not specified in POSIX.1-2001,
166 and is not widely available on other systems.
168 POSIX.1-2008 specifies that \(aqb\(aq in
171 However, Technical Corrigendum 1
172 .\" http://austingroupbugs.net/view.php?id=396
173 adjusts the standard to allow implementation-specific treatment for this case,
174 thus permitting the glibc treatment of \(aqb\(aq.
176 There is no file descriptor associated with the file stream
177 returned by this function
180 will return an error if called on the returned stream).
182 With version 2.22, binary mode (see below) was removed,
183 many longstanding bugs in the implementation of
185 were fixed, and a new versioned symbol was created for this interface.
188 From version 2.9 to 2.21, the glibc implementation of
190 supported a "binary" mode,
191 enabled by specifying the letter \(aqb\(aq as the second character in
194 writes don't implicitly add a terminating null byte, and
197 is relative to the end of the buffer (i.e., the value specified by the
199 argument), rather than the current string length.
201 An API bug afflicted the implementation of binary mode:
202 to specify binary mode, the \(aqb\(aq must be the
206 Thus, for example, "wb+" has the desired effect, but "w+b" does not.
207 This is inconsistent with the treatment of
208 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=12836
213 Binary mode was removed in glibc 2.22; a \(aqb\(aq specified in
217 In versions of glibc before 2.22, if
219 is specified as zero,
223 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=11216
224 It would be more consistent if this case successfully created
225 a stream that then returned end-of-file on the first attempt at reading;
226 since version 2.22, the glibc implementation provides that behavior.
228 In versions of glibc before 2.22,
229 specifying append mode ("a" or "a+") for
231 sets the initial buffer position to the first null byte, but
232 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=13152
233 (if the current position is reset to a location other than
234 the end of the stream)
235 does not force subsequent writes to append at the end of the stream.
236 This bug is fixed in glibc 2.22.
238 In versions of glibc before 2.22, if the
242 specifies append ("a" or "a+"), and the
244 argument does not cover a null byte in
246 then, according to POSIX.1-2008,
247 the initial buffer position should be set to
248 the next byte after the end of the buffer.
249 However, in this case the glibc
250 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=13151
252 sets the buffer position to \-1.
253 This bug is fixed in glibc 2.22.
255 In versions of glibc before 2.22,
256 .\" https://sourceware.org/bugzilla/show_bug.cgi?id=14292
263 was performed on a stream created by
269 from the end-of-stream position, instead of being added.
270 This bug is fixed in glibc 2.22.
272 The glibc 2.9 addition of "binary" mode for
274 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=6544
275 silently changed the ABI: previously,
280 The program below uses
282 to open an input buffer, and
283 .BR open_memstream (3)
284 to open a dynamically sized output buffer.
285 The program scans its input string (taken from the program's
286 first command-line argument) reading integers,
287 and writes the squares of these integers to the output buffer.
288 An example of the output produced by this program is the following:
292 .RB "$" " ./a.out \(aq1 23 43\(aq"
293 size=11; ptr=1 529 1849
304 #define handle_error(msg) \e
305 do { perror(msg); exit(EXIT_FAILURE); } while (0)
308 main(int argc, char *argv[])
316 fprintf(stderr, "Usage: %s \(aq<num>...\(aq\en", argv[0]);
320 in = fmemopen(argv[1], strlen(argv[1]), "r");
322 handle_error("fmemopen");
324 out = open_memstream(&ptr, &size);
326 handle_error("open_memstream");
329 s = fscanf(in, "%d", &v);
333 s = fprintf(out, "%d ", v * v);
335 handle_error("fprintf");
341 printf("size=%zu; ptr=%s\en", size, ptr);
350 .BR open_memstream (3)