C Reference ยท stdio.h
C fmemopen() and open_memstream() functions
Reference for POSIX fmemopen() and open_memstream(): treat a memory buffer as a FILE stream, read it with fgets or collect printf output in a growing string.
fmemopen() and open_memstream() give you a FILE * that reads from or writes to memory instead of a disk file. Because every stdio function works on a FILE *, you can feed a string to code that expects a file, or capture what fprintf() writes into a string that grows by itself.
Both come from POSIX.1-2008, not from ISO C, so under -std=c23 you must define _POSIX_C_SOURCE 200809L before the first include. They exist on Linux and other POSIX systems but not in the Windows C runtime.
#define _POSIX_C_SOURCE 200809L #include <stdio.h> FILE *fmemopen(void *buf, size_t size, const char *mode); FILE *open_memstream(char **bufp, size_t *sizep);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
FILE *fmemopen(void *buf, size_t size, const char *mode); FILE *open_memstream(char **bufp, size_t *sizep);
| Name | Description |
|---|---|
buf |
Memory the stream uses. If NULL, fmemopen() allocates size bytes itself (the mode must then allow both reading and writing, e.g. "w+"). |
size |
Size of the buffer in bytes (must be greater than zero). |
mode |
"r", "w", "a", "r+", "w+" or "a+", like fopen(). Mode "w" makes the buffer start empty (a NUL is stored at its start). |
bufp, sizep |
Where open_memstream() stores the address and length of the dynamically allocated buffer. |
Returns: a new FILE *, or NULL with errno set on failure.
Errors / undefined behavior: reading beyond size gives end-of-file; writing beyond it fails. With open_memstream() the values in *bufp and *sizep are valid only after fflush() or fclose() and only until the next write; do not modify them yourself. You must free(*bufp) after fclose(). Using the buffer after the stream is closed (for fmemopen with a caller buffer) is fine, but freeing it before is not.
Usage notes
fmemopenwith a read mode treats the buffer as a file of exactlysizebytes; null bytes are ordinary data. Passstrlen(text)and notsizeof textto avoid the terminator.fmemopenwith a write mode writes a terminating NUL after the data when you flush or close (if there is room), so the buffer works as a C string.open_memstreamreplacessprintfloops. The buffer grows automatically, so there is no need to compute the size in advance or usesnprintf()twice.- Wide variant:
open_wmemstream()collects wide characters. - Testing: these functions let you unit-test code that takes a
FILE *without touching the disk, and feed canned input to parsers. - Seeking works inside the allotted memory with
fseek(). - Portability: not in ISO C23 and not on Windows (MSVC); on macOS older than 10.13 they are missing. Use
tmpfile()as a portable fallback. - Nothing is shared with other processes; the "file" lives only in your program's memory.
Related
tmpfile,fprintfanddprintf,sprintfandsnprintf,getline,FILE- Lessons: Text files, User input