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       #define _GNU_SOURCE
10       #include <stdio.h>
11
12       FILE *fmemopen(void *buf, size_t size, const char *mode);
13
14       FILE *open_memstream(char **ptr, size_t *sizeloc);
15
16       #define _GNU_SOURCE
17       #include <wchar.h>
18
19       FILE *open_wmemstream(wchar_t **ptr, size_t *sizeloc);
20

DESCRIPTION

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

RETURN VALUE

86       Upon successful completion fmemopen(), open_memstream() and  open_wmem‐
87       stream()  return a FILE pointer.  Otherwise, NULL is returned and errno
88       is set to indicate the error.
89

VERSIONS

91       fmemopen() and open_memstream() were already available in glibc  1.0.x.
92       open_wmemstream() is available since glibc 2.4.
93

CONFORMING TO

95       POSIX.1-2008.   These  functions are not specified in POSIX.1-2001, and
96       are not widely available on other systems.
97

NOTES

99       There is no file descriptor associated with the file stream returned by
100       these  functions (i.e., fileno(3) will return an error if called on the
101       returned stream).
102

BUGS

104       In glibc before version 2.7, seeking past the end of a  stream  created
105       by  open_memstream()  does  not enlarge the buffer; instead the fseek()
106       call fails, returning -1.
107

EXAMPLE

109       The program  below  uses  fmemopen()  to  open  an  input  buffer,  and
110       open_memstream()  to  open a dynamically sized output buffer.  The pro‐
111       gram scans its input string (taken from the  program's  first  command-
112       line  argument) reading integers, and writes the squares of these inte‐
113       gers to the output buffer.  An example of the output produced  by  this
114       program is the following:
115
116           $ ./a.out '1 23 43'
117           size=11; ptr=1 529 1849
118
119   Program source
120
121       #define _GNU_SOURCE
122       #include <assert.h>
123       #include <string.h>
124       #include <stdio.h>
125       #include <stdlib.h>
126
127       #define handle_error(msg) \
128           do { perror(msg); exit(EXIT_FAILURE); } while (0)
129
130       int
131       main(int argc, char *argv[])
132       {
133           FILE *out, *in;
134           int v, s;
135           size_t size;
136           char *ptr;
137
138           assert(argc == 2);
139
140           in = fmemopen(argv[1], strlen(argv[1]), "r");
141           if (in == NULL)
142               handle_error("fmemopen");
143
144           out = open_memstream(&ptr, &size);
145           if (out == NULL)
146               handle_error("fmemopen");
147
148           for (;;) {
149               s = fscanf(in, "%d", &v);
150               if (s <= 0)
151                   break;
152
153               s = fprintf(out, "%d ", v * v);
154               if (s == -1)
155                   handle_error("fprintf");
156           }
157           fclose(in);
158           fclose(out);
159           printf("size=%ld; ptr=%s\n", (long) size, ptr);
160           free(ptr);
161           exit(EXIT_SUCCESS);
162       }
163

SEE ALSO

165       fopen(3), fopencookie(3), feature_test_macros(7)
166

COLOPHON

168       This  page  is  part of release 3.22 of the Linux man-pages project.  A
169       description of the project, and information about reporting  bugs,  can
170       be found at http://www.kernel.org/doc/man-pages/.
171
172
173
174GNU                               2009-04-21                       FMEMOPEN(3)
Impressum