1FMEMOPEN(3)                     glibc function                     FMEMOPEN(3)
2
3
4

NAME

6       fmemopen, open_memstream -  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

DESCRIPTION

17       The  fmemopen()  function opens a stream that permits the access speci‐
18       fied by mode.  The stream allows I/O to be performed on the  string  or
19       memory  buffer  pointed  to  by buf.  This buffer must be at least size
20       bytes long.
21
22       The argument mode is the same as for fopen(3).  If  mode  specifies  an
23       append  mode,  then the initial file position is set to location of the
24       first null byte ('\0') in the buffer; otherwise the initial file  posi‐
25       tion is set to the start of the buffer.
26
27       When  a  stream that has been opened for writing is flushed (fflush(3))
28       or closed (fclose(3)), a null byte is written at the end of the  buffer
29       if  there  is  space.   The  caller should ensure that an extra byte is
30       available in the buffer (and that size counts that byte) to  allow  for
31       this.
32
33       Attempts  to  write  more  than  size  bytes to the buffer result in an
34       error.  (By default, such errors will only be visible  when  the  stdio
35       buffer  is  flushed.   Disabling buffering with setbuf(fp, NULL) may be
36       useful to detect errors at the time of an output  operation.   Alterna‐
37       tively,  the  caller can explicitly set buf as the stdio stream buffer,
38       at the same time informing stdio  of  the  buffer's  size,  using  set‐
39       buffer(fp, buf, size).)
40
41       In  a stream opened for reading, null bytes ('\0') in the buffer do not
42       cause read operations to return an end-of-file indication.  A read from
43       the  buffer  will  only  indicate  end-of-file  when  the  file pointer
44       advances size bytes past the start of the buffer.
45
46       If buf is specified as NULL, then fmemopen()  dynamically  allocates  a
47       buffer  size  bytes long.  This is useful for an application that wants
48       to write data to a temporary buffer and then read it back  again.   The
49       buffer is automatically freed when the stream is closed.  Note that the
50       caller has no way to obtain a pointer to the temporary buffer allocated
51       by this call (but see open_memstream() below).
52
53       The  open_memstream() opens a stream for writing to a buffer.  The buf‐
54       fer is dynamically allocated (as  with  malloc(3)),  and  automatically
55       grows as required.  After closing the stream, the caller should free(3)
56       this buffer.
57
58       When the stream is closed (fclose(3)) or flushed (fflush(3)), the loca‐
59       tions  pointed  to  by  ptr and sizeloc are updated to contain, respec‐
60       tively, a pointer to the buffer and the current  size  of  the  buffer.
61       These  values  remain valid only as long as the caller performs no fur‐
62       ther output on the stream.  If further output is  performed,  then  the
63       stream must again be flushed before trying to access these variables.
64
65       A  null  byte is maintained at the end of the buffer.  This byte is not
66       included in the size value stored at sizeloc.
67

RETURN VALUE

69       Upon successful completion fmemopen()  and  open_memstream()  return  a
70       FILE pointer.  Otherwise, NULL is  returned  and  the  global  variable
71       errno is set to indicate the error.
72
73

EXAMPLE

75       The program  below  uses  fmemopen()  to  open  an  input  buffer,  and
76       open_memstream()  to  open a dynamically sized output buffer.  The pro‐
77       gram scans its input string (taken from the  program's  first  command-
78       line  argument) reading integers, and writes the squares of these inte‐
79       gers to the output buffer.  An example of the output produced  by  this
80       program is the following:
81
82       $ ./a.out "1 23 43"
83       size=11; ptr=1 529 1849
84
85       #define _GNU_SOURCE
86       #include <assert.h>
87       #include <string.h>
88       #include <stdio.h>
89       #include <stdlib.h>
90
91       int main(int argc, char *argv[])
92       {
93           FILE *out, *in;
94           int v, s;
95           size_t size;
96           char *ptr;
97
98           assert(argc == 2);
99
100           in = fmemopen(argv[1], strlen(argv[1]), "r");
101           if (in == NULL) { perror("fmemopen"); exit(EXIT_FAILURE);}
102
103           out = open_memstream(&ptr, &size);
104           if (out == NULL) { perror("fmemopen"); exit(EXIT_FAILURE);}
105
106           for (;;) {
107               s = fscanf(in, "%d", &v);
108               if (s <= 0)
109                   break;
110
111               s = fprintf(out, "%d ", v * v);
112               if (s == -1) { perror("fprintf"); exit(EXIT_FAILURE); }
113           }
114           fclose(in);
115           fclose(out);
116           printf("size=%ld; ptr=%s\n", (long) size, ptr);
117           free(ptr);
118           exit(EXIT_SUCCESS);
119       }
120
121

CONFORMING TO

123       These functions are GNU extensions.
124

SEE ALSO

126       open(3), feature_test_macros(7)
127
128
129
130GNU                               2005-12-08                       FMEMOPEN(3)
Impressum