1FMEMOPEN(3)                Linux Programmer's Manual               FMEMOPEN(3)
2
3
4

NAME

6       fmemopen, open_memstream, open_wmemstream -  open memory as stream
7

SYNOPSIS

9       #include <stdio.h>
10
11       FILE *fmemopen(void *buf, size_t size, const char *mode);
12
13       FILE *open_memstream(char **ptr, size_t *sizeloc);
14
15       #include <wchar.h>
16
17       FILE *open_wmemstream(wchar_t **ptr, size_t *sizeloc);
18
19   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
20
21       fmemopen(), open_memstream(), open_wmemstream():
22           Since glibc 2.10:
23               _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
24           Before glibc 2.10:
25               _GNU_SOURCE
26

DESCRIPTION

28       The  fmemopen()  function opens a stream that permits the access speci‐
29       fied by mode.  The stream allows I/O to be performed on the  string  or
30       memory  buffer  pointed  to  by buf.  This buffer must be at least size
31       bytes long.
32
33       The argument mode is the same as for fopen(3).  If  mode  specifies  an
34       append  mode,  then the initial file position is set to the location of
35       the first null byte ('\0') in the buffer; otherwise  the  initial  file
36       position  is set to the start of the buffer.  Since glibc 2.9, the let‐
37       ter 'b' may be specified as the second character in  mode.   This  pro‐
38       vides  "binary"  mode:  writes  don't implicitly add a terminating null
39       byte, and fseek(3) SEEK_END is relative to the end of the buffer (i.e.,
40       the  value  specified  by  the  size argument), rather than the current
41       string length.
42
43       When a stream that has been opened for writing is  flushed  (fflush(3))
44       or  closed (fclose(3)), a null byte is written at the end of the buffer
45       if there is space.  The caller should ensure  that  an  extra  byte  is
46       available  in  the buffer (and that size counts that byte) to allow for
47       this.
48
49       Attempts to write more than size bytes  to  the  buffer  result  in  an
50       error.   (By  default,  such errors will be visible only when the stdio
51       buffer is flushed.  Disabling buffering with  setbuf(fp, NULL)  may  be
52       useful  to  detect errors at the time of an output operation.  Alterna‐
53       tively, the caller can explicitly set buf as the stdio  stream  buffer,
54       at  the  same  time  informing  stdio  of the buffer's size, using set‐
55       buffer(fp, buf, size).)
56
57       In a stream opened for reading, null bytes ('\0') in the buffer do  not
58       cause read operations to return an end-of-file indication.  A read from
59       the buffer  will  only  indicate  end-of-file  when  the  file  pointer
60       advances size bytes past the start of the buffer.
61
62       If  buf  is  specified as NULL, then fmemopen() dynamically allocates a
63       buffer size bytes long.  This is useful for an application  that  wants
64       to  write  data to a temporary buffer and then read it back again.  The
65       buffer is automatically freed when the stream is closed.  Note that the
66       caller has no way to obtain a pointer to the temporary buffer allocated
67       by this call (but see open_memstream() below).
68
69       The open_memstream() function opens a stream for writing to  a  buffer.
70       The  buffer is dynamically allocated (as with malloc(3)), and automati‐
71       cally grows as required.  After closing the stream, the  caller  should
72       free(3) this buffer.
73
74       When the stream is closed (fclose(3)) or flushed (fflush(3)), the loca‐
75       tions pointed to by ptr and sizeloc are  updated  to  contain,  respec‐
76       tively,  a  pointer  to  the buffer and the current size of the buffer.
77       These values remain valid only as long as the caller performs  no  fur‐
78       ther  output  on  the stream.  If further output is performed, then the
79       stream must again be flushed before trying to access these variables.
80
81       A null byte is maintained at the end of the buffer.  This byte  is  not
82       included in the size value stored at sizeloc.
83
84       The  stream's  file position can be changed with fseek(3) or fseeko(3).
85       Moving the file position past the end of the data already written fills
86       the intervening space with zeros.
87
88       The  open_wmemstream()  is similar to open_memstream(), but operates on
89       wide characters instead of bytes.
90

RETURN VALUE

92       Upon successful completion fmemopen(), open_memstream() and  open_wmem‐
93       stream()  return a FILE pointer.  Otherwise, NULL is returned and errno
94       is set to indicate the error.
95

VERSIONS

97       fmemopen() and open_memstream() were already available in glibc  1.0.x.
98       open_wmemstream() is available since glibc 2.4.
99

CONFORMING TO

101       POSIX.1-2008.   These  functions are not specified in POSIX.1-2001, and
102       are not widely available on other systems.
103
104       POSIX.1-2008 specifies that 'b' in mode  shall  be  ignored.   However,
105       Technical  Corrigendum  1 adjusts the standard to allow implementation-
106       specific treatment for this case, thus permitting the  glibc  treatment
107       of 'b'.
108

NOTES

110       There is no file descriptor associated with the file stream returned by
111       these functions (i.e., fileno(3) will return an error if called on  the
112       returned stream).
113

BUGS

115       In  glibc  before version 2.7, seeking past the end of a stream created
116       by open_memstream() does not enlarge the buffer; instead  the  fseek(3)
117       call fails, returning -1.
118
119       If  size  is specified as zero, fmemopen() fails with the error EINVAL.
120       It would be more consistent if this case successfully created a  stream
121       that  then  returned end of file on the first attempt at reading.  Fur‐
122       thermore, POSIX.1-2008 does not specify a failure for this case.
123
124       Specifying append mode ("a" or "a+") for fmemopen()  sets  the  initial
125       file  position to the first null byte, but (if the file offset is reset
126       to a location other than the end of the stream) does not  force  subse‐
127       quent writes to append at the end of the stream.
128
129       If  the mode argument to fmemopen() specifies append ("a" or "a+"), and
130       the size argument does not cover a null byte in buf then, according  to
131       POSIX.1-2008,  the initial file position should be set to the next byte
132       after the end of the buffer.  However, in this case  the  glibc  fmemo‐
133       pen() sets the file position to -1.
134
135       To  specify binary mode for fmemopen() the 'b' must be the second char‐
136       acter in mode.  Thus, for example, "wb+" has the  desired  effect,  but
137       "w+b"  does  not.   This  is inconsistent with the treatment of mode by
138       fopen(3).
139
140       The glibc 2.9 addition of "binary" mode for fmemopen() silently changed
141       the ABI: previously, fmemopen() ignored 'b' in mode.
142

EXAMPLE

144       The  program  below  uses  fmemopen()  to  open  an  input  buffer, and
145       open_memstream() to open a dynamically sized output buffer.   The  pro‐
146       gram  scans  its  input string (taken from the program's first command-
147       line argument) reading integers, and writes the squares of these  inte‐
148       gers  to  the output buffer.  An example of the output produced by this
149       program is the following:
150
151           $ ./a.out '1 23 43'
152           size=11; ptr=1 529 1849
153
154   Program source
155
156       #define _GNU_SOURCE
157       #include <string.h>
158       #include <stdio.h>
159       #include <stdlib.h>
160
161       #define handle_error(msg) \
162           do { perror(msg); exit(EXIT_FAILURE); } while (0)
163
164       int
165       main(int argc, char *argv[])
166       {
167           FILE *out, *in;
168           int v, s;
169           size_t size;
170           char *ptr;
171
172           if (argc != 2) {
173            fprintf(stderr, "Usage: %s <file>\n", argv[0]);
174            exit(EXIT_FAILURE);
175           }
176
177           in = fmemopen(argv[1], strlen(argv[1]), "r");
178           if (in == NULL)
179               handle_error("fmemopen");
180
181           out = open_memstream(&ptr, &size);
182           if (out == NULL)
183               handle_error("open_memstream");
184
185           for (;;) {
186               s = fscanf(in, "%d", &v);
187               if (s <= 0)
188                   break;
189
190               s = fprintf(out, "%d ", v * v);
191               if (s == -1)
192                   handle_error("fprintf");
193           }
194           fclose(in);
195           fclose(out);
196           printf("size=%ld; ptr=%s\n", (long) size, ptr);
197           free(ptr);
198           exit(EXIT_SUCCESS);
199       }
200

SEE ALSO

202       fopen(3), fopencookie(3)
203

COLOPHON

205       This page is part of release 3.53 of the Linux  man-pages  project.   A
206       description  of  the project, and information about reporting bugs, can
207       be found at http://www.kernel.org/doc/man-pages/.
208
209
210
211GNU                               2012-04-28                       FMEMOPEN(3)
Impressum