OPEN_MEMSTREAM(3)          POSIX Programmer's Manual         OPEN_MEMSTREAM(3)



PPRROOLLOOGG
       This manual page is part of the POSIX Programmer's Manual.  The OS
       implementation of this interface may differ (consult the corresponding
       OS manual page for details of OS behavior), or the interface may not be
       implemented on OS.


NNAAMMEE
       open_memstream, open_wmemstream -- open a dynamic memory buffer stream

SSYYNNOOPPSSIISS
       #include <stdio.h>

       FILE *open_memstream(char **_b_u_f_p, size_t *_s_i_z_e_p);

       #include <wchar.h>

       FILE *open_wmemstream(wchar_t **_b_u_f_p, size_t *_s_i_z_e_p);

DDEESSCCRRIIPPTTIIOONN
       The _o_p_e_n___m_e_m_s_t_r_e_a_m() and _o_p_e_n___w_m_e_m_s_t_r_e_a_m() functions shall create an
       I/O stream associated with a dynamically allocated memory buffer. The
       stream shall be opened for writing and shall be seekable.

       The stream associated with a call to _o_p_e_n___m_e_m_s_t_r_e_a_m() shall be byte-
       oriented.

       The stream associated with a call to _o_p_e_n___w_m_e_m_s_t_r_e_a_m() shall be wide-
       oriented.

       The stream shall maintain a current position in the allocated buffer
       and a current buffer length. The position shall be initially set to
       zero (the start of the buffer). Each write to the stream shall start at
       the current position and move this position by the number of
       successfully written bytes for _o_p_e_n___m_e_m_s_t_r_e_a_m() or the number of
       successfully written wide characters for _o_p_e_n___w_m_e_m_s_t_r_e_a_m().  The length
       shall be initially set to zero. If a write moves the position to a
       value larger than the current length, the current length shall be set
       to this position. In this case a null character for _o_p_e_n___m_e_m_s_t_r_e_a_m() or
       a null wide character for _o_p_e_n___w_m_e_m_s_t_r_e_a_m() shall be appended to the
       current buffer. For both functions the terminating null is not included
       in the calculation of the buffer length.

       After a successful _f_f_l_u_s_h() or _f_c_l_o_s_e(), the pointer referenced by _b_u_f_p
       shall contain the address of the buffer, and the variable pointed to by
       _s_i_z_e_p shall contain the smaller of the current buffer length and the
       number of bytes for _o_p_e_n___m_e_m_s_t_r_e_a_m(), or the number of wide characters
       for _o_p_e_n___w_m_e_m_s_t_r_e_a_m(), between the beginning of the buffer and the
       current file position indicator.

       After a successful _f_f_l_u_s_h() the pointer referenced by _b_u_f_p and the
       variable referenced by _s_i_z_e_p remain valid only until the next write
       operation on the stream or a call to _f_c_l_o_s_e().

RREETTUURRNN VVAALLUUEE
       Upon successful completion, these functions shall return a pointer to
       the object controlling the stream. Otherwise, a null pointer shall be
       returned, and _e_r_r_n_o shall be set to indicate the error.

EERRRROORRSS
       These functions may fail if:

       EEIINNVVAALL _b_u_f_p or _s_i_z_e_p are NULL.

       EEMMFFIILLEE {FOPEN_MAX} streams are currently open in the calling process.

       EENNOOMMEEMM Memory for the stream or the buffer could not be allocated.

       _T_h_e _f_o_l_l_o_w_i_n_g _s_e_c_t_i_o_n_s _a_r_e _i_n_f_o_r_m_a_t_i_v_e_.

EEXXAAMMPPLLEESS
           ##iinncclluuddee <<ssttddiioo..hh>>
           ##iinncclluuddee <<ssttddlliibb..hh>>

           int
           main (void)
           {
               FILE *stream;
               char *buf;
               size_t len;
               off_t eob;

               stream = open_memstream (&buf, &len);
               if (stream == NULL)
                   /* handle error */ ;
               fprintf (stream, "hello my world");
               fflush (stream);
               printf ("buf=%s, len=%zu\n", buf, len);
               eob = ftello(stream);
               fseeko (stream, 0, SEEK_SET);
               fprintf (stream, "good-bye");
               fseeko (stream, eob, SEEK_SET);
               fclose (stream);
               printf ("buf=%s, len=%zu\n", buf, len);
               free (buf);
               return 0;
           }

       This program produces the following output:

           bbuuff==hheelllloo mmyy wwoorrlldd,, lleenn==1144
           bbuuff==ggoooodd--bbyyee wwoorrlldd,, lleenn==1144

AAPPPPLLIICCAATTIIOONN UUSSAAGGEE
       The buffer created by these functions should be freed by the
       application after closing the stream, by means of a call to _f_r_e_e().

RRAATTIIOONNAALLEE
       These functions are similar to _f_m_e_m_o_p_e_n() except that the memory is
       always allocated dynamically by the function, and the stream is opened
       only for output.

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       None.

SSEEEE AALLSSOO
       _f_c_l_o_s_e(), _f_d_o_p_e_n(), _f_f_l_u_s_h(), _f_m_e_m_o_p_e_n(), _f_o_p_e_n(), _f_r_e_e(), _f_r_e_o_p_e_n()

       The Base Definitions volume of POSIX.1-2008, <<ssttddiioo..hh>>, <<wwcchhaarr..hh>>

CCOOPPYYRRIIGGHHTT
       Portions of this text are reprinted and reproduced in electronic form
       from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
       -- Portable Operating System Interface (POSIX), The Open Group Base
       Specifications Issue 7, Copyright (C) 2013 by the Institute of
       Electrical and Electronics Engineers, Inc and The Open Group.  (This is
       POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
       event of any discrepancy between this version and the original IEEE and
       The Open Group Standard, the original IEEE and The Open Group Standard
       is the referee document. The original Standard can be obtained online
       at http://www.unix.org/online.html .

       Any typographical or formatting errors that appear in this page are
       most likely to have been introduced during the conversion of the source
       files to man page format. To report such errors, see
       https://www.kernel.org/doc/man-pages/reporting_bugs.html .



IEEE/The Open Group                  2013                    OPEN_MEMSTREAM(3)
