1OPEN_MEMSTREAM(3P)         POSIX Programmer's Manual        OPEN_MEMSTREAM(3P)
2
3
4

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10
11

NAME

13       open_memstream, open_wmemstream — open a dynamic memory buffer stream
14

SYNOPSIS

16       #include <stdio.h>
17
18       FILE *open_memstream(char **bufp, size_t *sizep);
19
20       #include <wchar.h>
21
22       FILE *open_wmemstream(wchar_t **bufp, size_t *sizep);
23

DESCRIPTION

25       The open_memstream() and open_wmemstream() functions  shall  create  an
26       I/O  stream  associated with a dynamically allocated memory buffer. The
27       stream shall be opened for writing and shall be seekable.
28
29       The stream associated with a call to open_memstream()  shall  be  byte-
30       oriented.
31
32       The  stream  associated with a call to open_wmemstream() shall be wide-
33       oriented.
34
35       The stream shall maintain a current position in  the  allocated  buffer
36       and  a  current  buffer  length. The position shall be initially set to
37       zero (the start of the buffer). Each write to the stream shall start at
38       the  current  position and move this position by the number of success‐
39       fully written bytes for open_memstream() or the number of  successfully
40       written  wide  characters  for  open_wmemstream().  The length shall be
41       initially set to zero. If a write moves the position to a value  larger
42       than  the current length, the current length shall be set to this posi‐
43       tion. In this case a null character for open_memstream() or a null wide
44       character  for  open_wmemstream() shall be appended to the current buf‐
45       fer. For both functions the terminating null is  not  included  in  the
46       calculation of the buffer length.
47
48       After a successful fflush() or fclose(), the pointer referenced by bufp
49       shall contain the address of the buffer, and the variable pointed to by
50       sizep  shall  contain  the smaller of the current buffer length and the
51       number of bytes for open_memstream(), or the number of wide  characters
52       for open_wmemstream(), between the beginning of the buffer and the cur‐
53       rent file position indicator.
54
55       After a successful fflush() the pointer  referenced  by  bufp  and  the
56       variable  referenced  by  sizep  remain valid only until the next write
57       operation on the stream or a call to fclose().
58

RETURN VALUE

60       Upon successful completion, these functions shall return a  pointer  to
61       the  object  controlling the stream. Otherwise, a null pointer shall be
62       returned, and errno shall be set to indicate the error.
63

ERRORS

65       These functions may fail if:
66
67       EINVAL bufp or sizep are NULL.
68
69       EMFILE {FOPEN_MAX} streams are currently open in the calling process.
70
71       ENOMEM Memory for the stream or the buffer could not be allocated.
72
73       The following sections are informative.
74

EXAMPLES

76           #include <stdio.h>
77           #include <stdlib.h>
78
79           int
80           main (void)
81           {
82               FILE *stream;
83               char *buf;
84               size_t len;
85               off_t eob;
86
87               stream = open_memstream (&buf, &len);
88               if (stream == NULL)
89                   /* handle error */ ;
90               fprintf (stream, "hello my world");
91               fflush (stream);
92               printf ("buf=%s, len=%zu\n", buf, len);
93               eob = ftello(stream);
94               fseeko (stream, 0, SEEK_SET);
95               fprintf (stream, "good-bye");
96               fseeko (stream, eob, SEEK_SET);
97               fclose (stream);
98               printf ("buf=%s, len=%zu\n", buf, len);
99               free (buf);
100               return 0;
101           }
102
103       This program produces the following output:
104
105           buf=hello my world, len=14
106           buf=good-bye world, len=14
107

APPLICATION USAGE

109       The buffer created by these functions should be freed by  the  applica‐
110       tion after closing the stream, by means of a call to free().
111

RATIONALE

113       These  functions  are  similar  to fmemopen() except that the memory is
114       always allocated dynamically by the function, and the stream is  opened
115       only for output.
116

FUTURE DIRECTIONS

118       None.
119

SEE ALSO

121       fclose(), fdopen(), fflush(), fmemopen(), fopen(), free(), freopen()
122
123       The Base Definitions volume of POSIX.1‐2008, <stdio.h>, <wchar.h>
124
126       Portions  of  this text are reprinted and reproduced in electronic form
127       from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
128       --  Portable  Operating  System  Interface (POSIX), The Open Group Base
129       Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
130       cal  and  Electronics  Engineers,  Inc  and  The  Open Group.  (This is
131       POSIX.1-2008 with the 2013 Technical Corrigendum  1  applied.)  In  the
132       event of any discrepancy between this version and the original IEEE and
133       The Open Group Standard, the original IEEE and The Open Group  Standard
134       is  the  referee document. The original Standard can be obtained online
135       at http://www.unix.org/online.html .
136
137       Any typographical or formatting errors that appear  in  this  page  are
138       most likely to have been introduced during the conversion of the source
139       files to man page format. To report such errors,  see  https://www.ker
140       nel.org/doc/man-pages/reporting_bugs.html .
141
142
143
144IEEE/The Open Group                  2013                   OPEN_MEMSTREAM(3P)
Impressum