FMEMOPEN(3)

NAME
4.16 `fmemopen'--open a stream around a fixed-length string

SYNOPSIS
     #include <stdio.h>
     FILE *fmemopen(void *restrict BUF, size_t SIZE,
         const char *restrict MODE);

DESCRIPTION
`fmemopen' creates a seekable `FILE' stream that wraps a fixed-length buffer of SIZE bytes starting at BUF. The stream is opened with MODE treated as in `fopen', where append mode starts writing at the first NUL byte. If BUF is NULL, then SIZE bytes are automatically provided as if by `malloc', with the initial size of 0, and MODE must contain `+' so that data can be read after it is written.
   The stream maintains a current position, which moves according to bytes read or written, and which can be one past the end of the array. The stream also maintains a current file size, which is never greater than SIZE. If MODE starts with `r', the position starts at `0', and file size starts at SIZE if BUF was provided. If MODE starts with `w', the position and file size start at `0', and if BUF was provided, the first byte is set to NUL. If MODE starts with `a', the position and file size start at the location of the first NUL byte, or else SIZE if BUF was provided.
   When reading, NUL bytes have no significance, and reads cannot exceed the current file size. When writing, the file size can increase up to SIZE as needed, and NUL bytes may be embedded in the stream (see `open_memstream' for an alternative that automatically enlarges the buffer). When the stream is flushed or closed after a write that changed the file size, a NUL byte is written at the current position if there is still room; if the stream is not also open for reading, a NUL byte is additionally written at the last byte of BUF when the stream has exceeded SIZE, so that a write-only BUF is always NUL-terminated when the stream is flushed or closed (and the initial SIZE should take this into account). It is not possible to seek outside the bounds of SIZE. A NUL byte 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 SIZE is zero or MODE is invalid, ENOMEM if BUF was NULL and memory could not be allocated, or EMFILE if too many streams are already open.

PORTABILITY
This function is being added to POSIX 200x, but is not in POSIX 2001.
   Supporting OS subroutines required: `sbrk'.

SEE ALSO
fmemopen 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.