Section: Linux Programmer's Manual (3)
open_memstream, open_wmemstream - open a dynamic memory buffer stream
FILE *open_memstream(char **ptr, size_t *sizeloc);
FILE *open_wmemstream(wchar_t **ptr, size_t *sizeloc);
Feature Test Macro Requirements for glibc (see
- Since glibc 2.10:
_POSIX_C_SOURCE >= 200809L
- Before glibc 2.10:
function opens a stream for writing to a memory buffer.
The function dynamically allocates the buffer,
and the buffer automatically grows as needed.
Initially, the buffer has a size of zero.
After closing the stream, the caller should
The locations pointed to by
are used to report, respectively,
the current location and the size of the buffer.
The locations referred to by these pointers are updated
each time the stream is flushed
and when the stream is closed
These values remain valid only as long as the caller
performs no further output on the stream.
If further output is performed, then the stream
must again be flushed before trying to access these values.
A null byte is maintained at the end of the buffer.
This byte is
included in the size value stored at
The stream maintains the notion of a current position,
which is initially zero (the start of the buffer).
Each write operation implicitly adjusts the buffer position.
The stream's buffer position can be explicitly changed with
Moving the buffer position past the end
of the data already written fills the intervening space with
is similar to
but operates on wide characters instead of bytes.
Upon successful completion,
Otherwise, NULL is returned and
is set to indicate the error.
was already available in glibc 1.0.x.
is available since glibc 2.4.
For an explanation of the terms used in this section, see
These functions are not specified in POSIX.1-2001,
and are not widely available on other systems.
There is no file descriptor associated with the file stream
returned by these functions
will return an error if called on the returned stream).
In glibc before version 2.7, seeking past the end of a stream created by
does not enlarge the buffer; instead the
call fails, returning -1.
This page is part of release 5.02 of the Linux
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at