table of contents
other versions
- wheezy 3.44-1
- jessie 3.74-1
- jessie-backports 4.10-2~bpo8+1
- testing 4.10-2
- unstable 4.10-2
FMEMOPEN(3) | Linux Programmer's Manual | FMEMOPEN(3) |
NAME¶
fmemopen, open_memstream, open_wmemstream - open memory as streamSYNOPSIS¶
#include <stdio.h> FILE *fmemopen(void *buf, size_t size, const char *mode); FILE *open_memstream(char **ptr, size_t *sizeloc); #include <wchar.h> FILE *open_wmemstream(wchar_t **ptr, size_t *sizeloc);
Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
- Since glibc 2.10:
- _XOPEN_SOURCE >= 700 || _POSIX_C_SOURCE >= 200809L
- Before glibc 2.10:
- _GNU_SOURCE
DESCRIPTION¶
The fmemopen() function opens a stream that permits the access specified by mode. The stream allows I/O to be performed on the string or memory buffer pointed to by buf. This buffer must be at least size bytes long. The argument mode is the same as for fopen(3). If mode specifies an append mode, then the initial file position is set to the location of the first null byte ('\0') in the buffer; otherwise the initial file position is set to the start of the buffer. Since glibc 2.9, the letter 'b' may be specified as the second character in mode. This provides "binary" mode: writes don't implicitly add a terminating null byte, and fseek(3) SEEK_END is relative to the end of the buffer (i.e., the value specified by the size argument), rather than the current string length. When a stream that has been opened for writing is flushed (fflush(3)) or closed (fclose(3)), a null byte is written at the end of the buffer if there is space. The caller should ensure that an extra byte is available in the buffer (and that size counts that byte) to allow for this.RETURN VALUE¶
Upon successful completion fmemopen(), open_memstream() and open_wmemstream() return a FILE pointer. Otherwise, NULL is returned and errno is set to indicate the error.VERSIONS¶
fmemopen() and open_memstream() were already available in glibc 1.0.x. open_wmemstream() is available since glibc 2.4.CONFORMING TO¶
POSIX.1-2008. These functions are not specified in POSIX.1-2001, and are not widely available on other systems.NOTES¶
There is no file descriptor associated with the file stream returned by these functions (i.e., fileno(3) will return an error if called on the returned stream).BUGS¶
In glibc before version 2.7, seeking past the end of a stream created by open_memstream() does not enlarge the buffer; instead the fseek(3) call fails, returning -1.EXAMPLE¶
The program below uses fmemopen() to open an input buffer, and open_memstream() to open a dynamically sized output buffer. The program scans its input string (taken from the program's first command-line argument) reading integers, and writes the squares of these integers to the output buffer. An example of the output produced by this program is the following:$ ./a.out '1 23 43' size=11; ptr=1 529 1849
Program source¶
#define _GNU_SOURCE #include <string.h> #include <stdio.h> #include <stdlib.h> #define handle_error(msg) \ do { perror(msg); exit(EXIT_FAILURE); } while (0) int main(int argc, char *argv[]) { FILE *out, *in; int v, s; size_t size; char *ptr; if (argc != 2) { fprintf(stderr, "Usage: %s <file>\n", argv[0]); exit(EXIT_FAILURE); } in = fmemopen(argv[1], strlen(argv[1]), "r"); if (in == NULL) handle_error("fmemopen"); out = open_memstream(&ptr, &size); if (out == NULL) handle_error("open_memstream"); for (;;) { s = fscanf(in, "%d", &v); if (s <= 0) break; s = fprintf(out, "%d ", v * v); if (s == -1) handle_error("fprintf"); } fclose(in); fclose(out); printf("size=%ld; ptr=%s\n", (long) size, ptr); free(ptr); exit(EXIT_SUCCESS); }
SEE ALSO¶
fopen(3), fopencookie(3)COLOPHON¶
This page is part of release 3.44 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/.2012-04-28 | GNU |