# fmemopen

## NAME



           fmemopen -  open memory as stream
    


## LIBRARY



           Standard C library (_libc_ , _-lc_)
    


## SYNOPSIS



           **#include <stdio.h>**
    
           **FILE *fmemopen(** size_t size;
                          **void** _buf_**[**_size_**], size_t** _size_**, const char ***_mode_**);**
    
       Feature Test Macro Requirements for glibc (see
       feature_test_macros(7)):
    
           **fmemopen**():
               Since glibc 2.10:
                   _POSIX_C_SOURCE >= 200809L
               Before glibc 2.10:
                   _GNU_SOURCE
    


## DESCRIPTION



           The **fmemopen**() function opens a stream that permits the access
           specified by _mode_.  The stream allows I/O to be performed on the
           string or memory buffer pointed to by _buf_.
    
           The _mode_ argument specifies the semantics of I/O on the stream,
           and is one of the following:
    
           _r_      The stream is opened for reading.
    
           _w_      The stream is opened for writing.
    
           _a_      Append; open the stream for writing, with the initial
                  buffer position set to the first null byte.
    
           _r+_     Open the stream for reading and writing.
    
           _w+_     Open the stream for reading and writing.  The buffer
                  contents are truncated (i.e., '\0' is placed in the first
                  byte of the buffer).
    
           _a+_     Append; open the stream for reading and writing, with the
                  initial buffer position set to the first null byte.
    
           The stream maintains the notion of a current position, the
           location where the next I/O operation will be performed.  The
           current position is implicitly updated by I/O operations.  It can
           be explicitly updated using fseek(3), and determined using
           ftell(3).  In all modes other than append, the initial position is
           set to the start of the buffer.  In append mode, if no null byte
           is found within the buffer, then the initial position is _size+1_.
    
           If _buf_ is specified as NULL, then **fmemopen**() allocates a buffer of
           _size_ bytes.  This is useful for an application that wants to write
           data to a temporary buffer and then read it back again.  The
           initial position is set to the start of the buffer.  The buffer is
           automatically freed when the stream is closed.  Note that the
           caller has no way to obtain a pointer to the temporary buffer
           allocated by this call (but see open_memstream(3)).
    
           If _buf_ is not NULL, then it should point to a buffer of at least
           _size_ bytes allocated by the caller.
    
           When a stream that has been opened for writing is flushed
           (fflush(3)) or closed (fclose(3)), a null byte is written at the
           end of the buffer if there is space.  The caller should ensure
           that an extra byte is available in the buffer (and that _size_
           counts that byte) to allow for this.
    
           In a stream opened for reading, null bytes ('\0') in the buffer do
           not cause read operations to return an end-of-file indication.  A
           read from the buffer will indicate end-of-file only when the
           current buffer position advances _size_ bytes past the start of the
           buffer.
    
           Write operations take place either at the current position (for
           modes other than append), or at the current size of the stream
           (for append modes).
    
           Attempts to write more than _size_ bytes to the buffer result in an
           error.  By default, such errors will be visible (by the absence of
           data) only when the _stdio_ buffer is flushed.  Disabling buffering
           with the following call may be useful to detect errors at the time
           of an output operation:
    
               setbuf(stream, NULL);
    


## RETURN VALUE



           Upon successful completion, **fmemopen**() returns a _FILE_ pointer.
           Otherwise, NULL is returned and _errno_ is set to indicate the
           error.
    


## ATTRIBUTES



           For an explanation of the terms used in this section, see
           attributes(7).
           ┌──────────────────────────────────────┬───────────────┬─────────┐
           │ **Interface** │ **Attribute** │ **Value** │
           ├──────────────────────────────────────┼───────────────┼─────────┤
           │ **fmemopen**(),                          │ Thread safety │ MT-Safe │
           └──────────────────────────────────────┴───────────────┴─────────┘
    


## STANDARDS



           POSIX.1-2008.
    


## NOTES



           There is no file descriptor associated with the file stream
           returned by this function (i.e., fileno(3) will return an error if
           called on the returned stream).
    


## BUGS



           Before glibc 2.22, if _size_ is specified as zero, **fmemopen**() fails
           with the error **EINVAL**.  It would be more consistent if this case
           successfully created a stream that then returned end-of-file on
           the first attempt at reading; since glibc 2.22, the glibc
           implementation provides that behavior.
    
           Before glibc 2.22, specifying append mode ("a" or "a+") for
           **fmemopen**() sets the initial buffer position to the first null
           byte, but (if the current position is reset to a location other
           than the end of the stream) does not force subsequent writes to
           append at the end of the stream.  This bug is fixed in glibc 2.22.
    
           Before glibc 2.22, if the _mode_ argument to **fmemopen**() specifies
           append ("a" or "a+"), and the _size_ argument does not cover a null
           byte in _buf_ , then, according to POSIX.1-2008, the initial buffer
           position should be set to the next byte after the end of the
           buffer.  However, in this case the glibc **fmemopen**() sets the
           buffer position to -1.  This bug is fixed in glibc 2.22.
    
           Before glibc 2.22, when a call to fseek(3) with a _whence_ value of
           **SEEK_END** was performed on a stream created by **fmemopen**(), the
           _offset_ was _subtracted_ from the end-of-stream position, instead of
           being added.  This bug is fixed in glibc 2.22.
    
           The glibc 2.9 addition of "binary" mode for **fmemopen**() silently
           changed the ABI: previously, **fmemopen**() ignored 'b' in _mode_.
    


## EXAMPLES



           The program below uses **fmemopen**() to open an input buffer, and
           open_memstream(3) to open a dynamically sized output buffer.  The
           program scans its input string (taken from the program's first
           command-line argument) reading integers, and writes the squares of
           these integers to the output buffer.  An example of the output
           produced by this program is the following:
    
               $ **./a.out '1 23 43'**
               size=11; ptr=1 529 1849
    
       **Program source**
    
           #define _GNU_SOURCE
           #include <err.h>
           #include <stdio.h>
           #include <stdlib.h>
           #include <string.h>
    
           int
           main(int argc, char *argv[])
           {
               FILE *out, *in;
               int v, s;
               size_t size;
               char *ptr;
    
               if (argc != 2) {
                   fprintf(stderr, "Usage: %s '<num>...'\n", argv[0]);
                   exit(EXIT_FAILURE);
               }
    
               in = fmemopen(argv[1], strlen(argv[1]), "r");
               if (in == NULL)
                   err(EXIT_FAILURE, "fmemopen");
    
               out = open_memstream(&ptr, &size);
               if (out == NULL)
                   err(EXIT_FAILURE, "open_memstream");
    
               for (;;) {
                   s = fscanf(in, "%d", &v);
                   if (s <= 0)
                       break;
    
                   s = fprintf(out, "%d ", v * v);
                   if (s == -1)
                       err(EXIT_FAILURE, "fprintf");
               }
    
               fclose(in);
               fclose(out);
    
               printf("size=%zu; ptr=%s\n", size, ptr);
    
               free(ptr);
               exit(EXIT_SUCCESS);
           }
    


* * *