fmemopen(3)
πΈ fmemopen(3) β okay so, open memory as stream π
hi bestie, welcome to the glow-up of fmemopen(3), a Standard C library (libc) situation. Buckle up, because this one is genuinely iconic: it turns a plain chunk of memory into a full-on stdio stream. π§
β¨ NAME β¨
fmemopen β open memory as stream. that's it, that's the whole name, and it's giving exactly what it says on the tin. π
π LIBRARY π
Standard C library (libc, -lc). link it and go. π«Ά
π SYNOPSIS π
#include <stdio.h>
FILE *fmemopen(size_t size;
void buf[size], size_t size, const char *mode);
π¦ Feature Test Macro Requirements for glibc (see feature_test_macros(7)) π¦
fmemopen():
Since glibc 2.10:
_POSIX_C_SOURCE >= 200809L
Before glibc 2.10:
_GNU_SOURCE
ngl bestie, those macros are not optional. define them or the compiler will simply pretend you never typed fmemopen. π
π DESCRIPTION π
So like, fmemopen() opens a stream that lets you do the access specified by mode. The stream lets I/O happen on the string or memory buffer that buf points at. memory IS the file now, periodt. π₯
The mode argument is what sets the semantics of I/O on the stream, and it's one of these:
rβ the stream is opened for reading. reader era. πwβ the stream is opened for writing. author era. βοΈaβ Append: open the stream for writing, with the initial buffer position set to the first null byte.r+β open the stream for reading and writing.w+β open the stream for reading and writing. The buffer contents are truncated (i.e.,'\0'is placed in the first byte of the buffer).a+β Append: open the stream for reading and writing, with the initial buffer position set to the first null byte.
The stream keeps the notion of a current position, aka the spot where the next I/O operation is gonna happen. That current position is implicitly updated by I/O operations (it moves on its own, main character behavior). You can explicitly update it with fseek(3), and find out where it is with ftell(3). In all modes other than append, the initial position is set to the start of the buffer. In append mode, if no null byte is found within the buffer, then the initial position is size+1. π
If buf is specified as NULL, then fmemopen() allocates a buffer of size bytes. This is super useful for an application that wants to write data to a temporary buffer and then read it back again. The initial position is set to the start of the buffer. The buffer is automatically freed when the stream is closed. Note that the caller has no way to obtain a pointer to the temporary buffer allocated by this call (but see open_memstream(3)). so close your eyes and trust the process. π
If buf is not NULL, then it should point to a buffer of at least size bytes allocated by the caller. your buffer, your responsibility bestie. π
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. yes, that means you need room for the lil guy. π±
In a stream opened for reading, null bytes ('\0') in the buffer do not cause read operations to return an end-of-file indication. A read from the buffer will indicate end-of-file only when the current buffer position advances size bytes past the start of the buffer. so the '\0's are just vibing in there, minding their business. β¨
Write operations take place either at the current position (for modes other than append), or at the current size of the stream (for append modes).
Attempts to write more than size bytes to the buffer result in an error. By default, such errors will be visible (by the absence of data) only when the stdio buffer is flushed. Disabling buffering with the following call may be useful to detect errors at the time of an output operation:
setbuf(stream, NULL);
lowkey important: sleeping on that setbuf() call means your error just... doesn't show up on time. π
π RETURN VALUE π
Upon successful completion, fmemopen() returns a FILE pointer. Otherwise, NULL is returned and errno is set to indicate the error. flop = NULL, slay = FILE *. π
π ATTRIBUTES π
For an explanation of the terms used in this section, see attributes(7).
fmemopen()β Thread safety: MT-Safe
yes she's MT-Safe. she's built different. π
β STANDARDS β
POSIX.1-2008.
π°οΈ HISTORY π°οΈ
glibc 1.0.x. POSIX.1-2008.
POSIX.1-2008 specifies that 'b' in mode shall be ignored. However, Technical Corrigendum 1 adjusts the standard to allow implementation-specific treatment for this case, thus permitting the glibc treatment of 'b'.
With glibc 2.22, binary mode (see below) was removed, many longstanding bugs in the implementation of fmemopen() were fixed, and a new versioned symbol was created for this interface.
π¦ Binary mode π¦
From glibc 2.9 to glibc 2.21, the glibc implementation of fmemopen() supported a "binary" mode, enabled by specifying the letter 'b' as the second character in mode. In this 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.
An API bug afflicted the implementation of binary mode: to specify binary mode, the 'b' must be the second character in mode. Thus, for example, "wb+" has the desired effect, but "w+b" does not. This is inconsistent with the treatment of mode by fopen(3). like bestie, the letter had a designated seat and if you moved it the whole thing stopped working. π
Binary mode was removed in glibc 2.22; a 'b' specified in mode has no effect. it's just decoration now, iykyk. π
π NOTES π
There is no file descriptor associated with the file stream returned by this function (i.e., fileno(3) will return an error if called on the returned stream). no fd, no drama, she simply doesn't have one. π«
π BUGS π
Before glibc 2.22, if size is specified as zero, fmemopen() fails with the error EINVAL. It would be more consistent if this case successfully created a stream that then returned end-of-file on the first attempt at reading; since glibc 2.22, the glibc implementation provides that behavior. glow-up achieved. β¨
Before glibc 2.22, specifying append mode ("a" or "a+") for fmemopen() sets the initial buffer position to the first null byte, but (if the current position is reset to a location other than the end of the stream) does not force subsequent writes to append at the end of the stream. This bug is fixed in glibc 2.22.
Before glibc 2.22, if the mode argument to fmemopen() specifies append ("a" or "a+"), and the size argument does not cover a null byte in buf, then, according to POSIX.1-2008, the initial buffer position should be set to the next byte after the end of the buffer. However, in this case the glibc fmemopen() sets the buffer position to -1. This bug is fixed in glibc 2.22. yeah, -1. not it. π
Before glibc 2.22, when a call to fseek(3) with a whence value of SEEK_END was performed on a stream created by fmemopen(), the offset was subtracted from the end-of-stream position, instead of being added. This bug is fixed in glibc 2.22.
The glibc 2.9 addition of "binary" mode for fmemopen() silently changed the ABI: previously, fmemopen() ignored 'b' in mode.
bestie if you're on a pre-2.22 glibc, honestly, upgrade. these bugs were a whole era nobody asked to relive. π
π₯ EXAMPLES π₯
The program below uses fmemopen() to open an input buffer, and open_memstream(3) 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
like come ON, squares of numbers, dynamically sized, chef's kiss. π
π Program source π
#define _GNU_SOURCE
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main(int argc, char *argv[])
{
FILE *out, *in;
int v, s;
size_t size;
char *ptr;
if (argc != 2) {
fprintf(stderr, "Usage: %s '<num>...'\n", argv[0]);
exit(EXIT_FAILURE);
}
in = fmemopen(argv[1], strlen(argv[1]), "r");
if (in == NULL)
err(EXIT_FAILURE, "fmemopen");
out = open_memstream(&ptr, &size);
if (out == NULL)
err(EXIT_FAILURE, "open_memstream");
for (;;) {
s = fscanf(in, "%d", &v);
if (s <= 0)
break;
s = fprintf(out, "%d ", v * v);
if (s == -1)
err(EXIT_FAILURE, "fprintf");
}
fclose(in);
fclose(out);
printf("size=%zu; ptr=%s\n", size, ptr);
free(ptr);
exit(EXIT_SUCCESS);
}
π SEE ALSO π
fopen(3), fopencookie(3), open_memstream(3)
okay that's the whole page, bestie. this one's from Linux man-pages 6.20, dated 2026-02-08. go forth and stream your memory. πβ¨π¦π
π¦ where this page comes from
- arch rolling: man-pages 6.20-1 Β· BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- π the page's license: GPL-1.0-or-later
- Copyright 2005-2016, Michael Kerrisk <mtk.manpages@gmail.com>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms