1 .\" Copyright 2005 walter harms (walter.harms@informatik.uni-oldenburg.de),
2 .\" and Copyright 2005 Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" Distributed under the GPL.
4 .\" 2008-12-04, Petr Baudis <pasky@suse.cz>: Document open_wmemstream()
6 .TH FMEMOPEN 3 2010-09-15 "GNU" "Linux Programmer's Manual"
8 fmemopen, open_memstream, open_wmemstream \- open memory as stream
13 .BI "FILE *fmemopen(void *"buf ", size_t "size ", const char *" mode ");"
15 .BI "FILE *open_memstream(char **" ptr ", size_t *" sizeloc );
19 .BI "FILE *open_wmemstream(wchar_t **" ptr ", size_t *" sizeloc );
23 Feature Test Macro Requirements for glibc (see
24 .BR feature_test_macros (7)):
28 .BR open_memstream (),
29 .BR open_wmemstream ():
35 _XOPEN_SOURCE\ >=\ 700 || _POSIX_C_SOURCE\ >=\ 200809L
45 function opens a stream that permits the access specified by
47 The stream allows I/O to be performed on the string or memory buffer
50 This buffer must be at least
60 specifies an append mode, then the initial file position is set to
61 the location of the first null byte (\(aq\\0\(aq) in the buffer;
62 otherwise the initial file position is set to the start of the buffer.
64 the letter 'b' may be specified as the second character in
66 This provides "binary" mode:
67 writes don't implicitly add a terminating null byte, and
70 is relative to the end of the buffer (i.e., the value specified by the
72 argument), rather than the current string length.
74 When a stream that has been opened for writing is flushed
78 a null byte is written at the end of the buffer if there is space.
79 The caller should ensure that an extra byte is available in the
86 Attempts to write more than
88 bytes to the buffer result in an error.
89 (By default, such errors will only be visible when the
92 Disabling buffering with
94 may be useful to detect errors at the time of an output operation.
95 Alternatively, the caller can explicitly set
97 as the stdio stream buffer, at the same time informing stdio
98 of the buffer's size, using
99 .IR "setbuffer(fp, buf, size)" .)
100 .\" See http://sourceware.org/bugzilla/show_bug.cgi?id=1995
102 .\" http://sources.redhat.com/ml/libc-alpha/2006-04/msg00064.html
104 In a stream opened for reading,
105 null bytes (\(aq\\0\(aq) in the buffer do not cause read
106 operations to return an end-of-file indication.
107 A read from the buffer will only indicate end-of-file
108 when the file pointer advances
110 bytes past the start of the buffer.
114 is specified as NULL, then
116 dynamically allocates a buffer
119 This is useful for an application that wants to write data to
120 a temporary buffer and then read it back again.
121 The buffer is automatically freed when the stream is closed.
122 Note that the caller has no way to obtain a pointer to the
123 temporary buffer allocated by this call (but see
124 .BR open_memstream ()
128 .BR open_memstream ()
129 function opens a stream for writing to a buffer.
131 is dynamically allocated (as with
133 and automatically grows as required.
134 After closing the stream, the caller should
138 When the stream is closed
142 the locations pointed to by
146 are updated to contain, respectively, a pointer to the buffer and the
147 current size of the buffer.
148 These values remain valid only as long as the caller
149 performs no further output on the stream.
150 If further output is performed, then the stream
151 must again be flushed before trying to access these variables.
153 A null byte is maintained at the end of the buffer.
156 included in the size value stored at
159 The stream's file position can be changed with
163 Moving the file position past the end
164 of the data already written fills the intervening space with
168 .BR open_wmemstream ()
170 .BR open_memstream (),
171 but operates on wide characters instead of bytes.
173 Upon successful completion
175 .BR open_memstream ()
177 .BR open_wmemstream ()
181 Otherwise, NULL is returned and
183 is set to indicate the error.
187 .BR open_memstream ()
188 were already available in glibc 1.0.x.
189 .BR open_wmemstream ()
190 is available since glibc 2.4.
193 These functions are not specified in POSIX.1-2001,
194 and are not widely available on other systems.
196 There is no file descriptor associated with the file stream
197 returned by these functions
200 will return an error if called on the returned stream).
202 In glibc before version 2.7, seeking past the end of a stream created by
203 .BR open_memstream ()
204 does not enlarge the buffer; instead the
206 call fails, returning \-1.
207 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=1996
209 The program below uses
211 to open an input buffer, and
212 .BR open_memstream ()
213 to open a dynamically sized output buffer.
214 The program scans its input string (taken from the program's
215 first command-line argument) reading integers,
216 and writes the squares of these integers to the output buffer.
217 An example of the output produced by this program is the following:
221 .RB "$" " ./a.out \(aq1 23 43\(aq"
222 size=11; ptr=1 529 1849
233 #define handle_error(msg) \\
234 do { perror(msg); exit(EXIT_FAILURE); } while (0)
237 main(int argc, char *argv[])
245 fprintf(stderr, "Usage: %s <file>\\n", argv[0]);
249 in = fmemopen(argv[1], strlen(argv[1]), "r");
251 handle_error("fmemopen");
253 out = open_memstream(&ptr, &size);
255 handle_error("open_memstream");
258 s = fscanf(in, "%d", &v);
262 s = fprintf(out, "%d ", v * v);
264 handle_error("fprintf");
268 printf("size=%ld; ptr=%s\\n", (long) size, ptr);
276 .BR feature_test_macros (7)