OPEN_MEMSTREAM(3)

NAME
4.42 `open_memstream', `open_wmemstream'--open a write stream around an arbitrary-length string

SYNOPSIS
     #include <stdio.h>
     FILE *open_memstream(char **restrict BUF,
         size_t *restrict SIZE);
     #include <wchar.h>
     FILE *open_wmemstream(wchar_t **restrict BUF,
         size_t *restrict SIZE);

DESCRIPTION
`open_memstream' creates a seekable, byte-oriented `FILE' stream that wraps an arbitrary-length buffer, created as if by `malloc'. The current contents of *BUF are ignored; this implementation uses *SIZE as a hint of the maximum size expected, but does not fail if the hint was wrong. The parameters BUF and SIZE are later stored through following any call to `fflush' or `fclose', set to the current address and usable size of the allocated string; although after fflush, the pointer is only valid until another stream operation that results in a write. Behavior is undefined if the user alters either *BUF or *SIZE prior to `fclose'.
   `open_wmemstream' is like `open_memstream' just with the associated stream being wide-oriented. The size set in SIZE in subsequent operations is the number of wide characters.
   The stream is write-only, since the user can directly read *BUF after a flush; see `fmemopen' for a way to wrap a string with a readable stream. The user is responsible for calling `free' on the final *BUF after `fclose'.
   Any time the stream is flushed, a NUL byte is written at the current position (but is not counted in the buffer length), so that the string is always NUL-terminated after at most *SIZE bytes (or wide characters in case of `open_wmemstream'). However, data previously written beyond the current stream offset is not lost, and the NUL value written during a flush is restored to its previous value when seeking elsewhere in the string.

RETURNS
The return value is an open FILE pointer on success. On error, `NULL' is returned, and `errno' will be set to EINVAL if BUF or SIZE is NULL, ENOMEM if memory could not be allocated, or EMFILE if too many streams are already open.

PORTABILITY
POSIX.1-2008
   Supporting OS subroutines required: `sbrk'.

SEE ALSO
open_memstream is part of the library. The full documentation for is maintained as a Texinfo manual. If info and are properly installed at your site, the command

    info

will give you access to the complete manual.