open_memstream(3)
β¨ open_memstream(3) β Library Functions Manual β¨
okay bestie, gather round π«Ά this is a page from the Library Functions Manual, and it's about the absolute cutest trick in stdio: streams that write into memory instead of a file. no disk, no drama, just vibes. π
π NAME π
open_memstream, open_wmemstream β open a dynamic memory buffer stream
π LIBRARY π
Standard C library (libc, -lc) β yeah, the one you already have. π
πΈ SYNOPSIS πΈ
#include <stdio.h>
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))
open_memstream(),open_wmemstream():- Since glibc 2.10:
_POSIX_C_SOURCE >= 200809L
- Before glibc 2.10:
_GNU_SOURCE
- Since glibc 2.10:
lowkey important: if you're on old glibc you gotta define _GNU_SOURCE first, no cap. otherwise she simply will not exist. π
π DESCRIPTION π
So open_memstream() opens a stream for writing to a memory buffer. The function dynamically allocates that buffer, and the buffer just automatically grows as needed β iconic, self-expanding behavior, we love to see it. Initially the buffer has a size of zero. After closing the stream, the caller should free(3) this buffer. yes bestie, you free it, she does not free herself. π
The locations pointed to by ptr and sizeloc are used to report, respectively, the current location and the size of the buffer. Those locations get updated each time the stream is flushed (fflush(3)) and when the stream is closed (fclose(3)). These values remain valid only as long as the caller performs no further output on the stream. If further output happens, then the stream must again be flushed before you try to access these values. like, re-flush or the numbers are stale, that's just the rules. π
A null byte is maintained at the end of the buffer. That byte is not included in the size value stored at sizeloc. π
The stream maintains the notion of a current position, which is initially zero (the start of the buffer). Each write operation implicitly adjusts the buffer position. The stream's buffer position can be explicitly changed with fseek(3) or fseeko(3). Moving the buffer position past the end of the data already written fills the intervening space with null characters. β¨
open_wmemstream() is similar to open_memstream(), but operates on wide characters instead of bytes. same energy, bigger units. π
β RETURN VALUE β
Upon successful completion, open_memstream() and open_wmemstream() return a FILE pointer. π«Ά Otherwise, NULL is returned and errno is set to indicate the error. check your errno, bestie, she's always trying to tell you something.
π¦ ATTRIBUTES π¦
For an explanation of the terms used in this section, see attributes(7).
- Interface:
open_memstream(),open_wmemstream()β Attribute: Thread safety β Value: MT-Safe
MT-Safe!! she's safe to call from multiple threads, periodt. π₯
π STANDARDS π
POSIX.1-2008. certified, official, on the syllabus. π
π HISTORY π
open_memstream()β glibc 1.0.x. she's been here since basically forever, a true elder.open_wmemstream()β glibc 2.4. the wide-char little sister arrived later. π§
π 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).
so don't go asking for a fileno β there isn't one, it's a memory stream, it's built different. π
βοΈ
π BUGS π
Before glibc 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. not the glow-up you wanted. π
π EXAMPLES π
See fmemopen(3). that's where the demo lives, go look babe. π
π₯ SEE ALSO π₯
fmemopen(3), fopen(3), setbuf(3)
and that's the whole page bestie, you ate. π β¨
signing off β this was Linux man-pages 6.20, dated 2026-02-08, and yes it's still open_memstream(3) at the bottom. mwah. π«ΆπΈ
π¦ 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