FMEMOPEN(3POSIX)
π FMEMOPEN(3POSIX) π β okay so, open a memory buffer stream
hey bestie β¨ this one lives in the POSIX Programmer's Manual, and it's giving very much "make a stream out of a chunk of memory" energy. iconic.
πΈ PROLOG πΈ
okay so first thing's first: this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ β so pls go consult the corresponding Linux manual page for the deets on Linux behavior. Orβ¦ it might not be implemented on Linux at all π. vibe check your platform first, fr fr.
π NAME π
fmemopen β open a memory buffer stream. that's it, that's the girl. π
β¨ SYNOPSIS β¨
#include <stdio.h>
FILE *fmemopen(void *restrict buf, size_t size,
const char *restrict mode);
π¦ DESCRIPTION π¦
So the fmemopen() function shall associate the buffer given by the buf and size arguments with a stream. the way that it just... binds them together. The buf argument shall be either a null pointer or point to a buffer that is at least size bytes long.
The mode argument points to a string. If the string is one of the following, the stream shall be opened in the indicated mode. Otherwise, the behavior is undefined β so like, don't be chaotic, bestie π.
rβ Open the stream for reading.wβ Open the stream for writing.aβ Append; open the stream for writing at the first null byte.r+β Open the stream for update (reading and writing).w+β Open the stream for update (reading and writing). Truncate the buffer contents. she's destructive, watch out.a+β Append; open the stream for update (reading and writing); the initial position is at the first null byte.
Implementations shall accept all mode strings allowed by fopen(), but the use of the character 'b' shall produce implementation-defined results, where the resulting FILE * need not behave the same as if 'b' were omitted. iykyk.
If a null pointer is specified as the buf argument, fmemopen() shall allocate size bytes of memory as if by a call to malloc(). This buffer shall be automatically freed when the stream is closed. And lowkey important: because this feature is only useful when the stream is opened for updating (because there is no way to get a pointer to the buffer), the fmemopen() call may fail if the mode argument does not include a '+'. π
The stream shall maintain a current position in the buffer. This position shall be initially set to either:
- the beginning of the buffer (for
randwmodes), or - the first null byte in the buffer (for
amodes).
If no null byte is found in append mode, the initial position shall be set to one byte after the end of the buffer.
If buf is a null pointer, the initial position shall always be set to the beginning of the buffer.
The stream shall also maintain the size of the current buffer contents; use of fseek() or fseeko() on the stream with SEEK_END shall seek relative to this size. got it? got it.
For modes r and r+ the size shall be set to the value given by the size argument. For modes w and w+ the initial size shall be zero, and for modes a and a+ the initial size shall be:
- Zero, if
bufis a null pointer - The position of the first null byte in the buffer, if one is found
- The value of the
sizeargument, ifbufis not a null pointer and no null byte is found
A read operation on the stream shall not advance the current buffer position beyond the current buffer size. Reaching the buffer size in a read operation shall count as ``end-of-file''. Null bytes in the buffer shall have no special meaning for reads. The read operation shall start at the current buffer position of the stream. β¨
A write operation shall start either at the current position of the stream (if mode has not specified 'a' as the first character) or at the current size of the stream (if mode had 'a' as the first character). If the current position at the end of the write is larger than the current buffer size, the current buffer size shall be set to the current position. A write operation on the stream shall not advance the current buffer size beyond the size given in the size argument. she stays in her lane, periodt.
When a stream open for writing is flushed or closed, a null byte shall be written at the current position or at the end of the buffer, depending on the size of the contents. If a stream open for update is flushed or closed and the last write has advanced the current buffer size, a null byte shall be written at the end of the buffer if it fits. π
An attempt to seek a memory buffer stream to a negative position or to a position larger than the buffer size given in the size argument shall fail. no cap, she will not do it.
π RETURN VALUE π
Upon successful completion, fmemopen() shall return a pointer to the object controlling the stream. slay. Otherwise, a null pointer shall be returned, and errno shall be set to indicate the error. check your errno, bestie, always.
π ERRORS π
The fmemopen() function shall fail if:
EMFILEβ{STREAM_MAX}streams are currently open in the calling process.
The fmemopen() function may fail if:
EINVALβ The value of themodeargument is not valid.EINVALβ Thebufargument is a null pointer and themodeargument does not include a'+'character.EINVALβ Thesizeargument specifies a buffer size of zero and the implementation does not support this.ENOMEMβ Thebufargument is a null pointer and the allocation of a buffer of lengthsizehas failed.EMFILEβ{FOPEN_MAX}streams are currently open in the calling process.
The following sections are informative. (translation: they're the vibe, not the law π )
π EXAMPLES π
#include <stdio.h>
#include <string.h>
static char buffer[] = "foobar";
int
main (void)
{
int ch;
FILE *stream;
stream = fmemopen(buffer, strlen (buffer), "r");
if (stream == NULL)
/* handle error */;
while ((ch = fgetc(stream)) != EOF)
printf("Got %c\n", ch);
fclose(stream);
return (0);
}
This program produces the following output:
Got f
Got o
Got o
Got b
Got a
Got r
ate and left no crumbs. π₯
π§ APPLICATION USAGE π§
None. nothing to see here, moving on, bestie. π
π RATIONALE π
This interface has been introduced to eliminate many of the errors encountered in the construction of strings, notably overflowing of strings. This interface prevents overflow. she's a bodyguard, honestly. π«Ά
β FUTURE DIRECTIONS β
- A future version of this standard may mandate specific behavior when the
modeargument includes'b'. - A future version of this standard may require support of zero-length buffer streams explicitly.
π SEE ALSO π
fdopen()fopen()freopen()fseek()malloc()open_memstream()
The Base Definitions volume of POSIX.1β2017, <stdio.h>
πΈ COPYRIGHT πΈ
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .
Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .
and that's the whole page, bestie!! mwah π this lil masterpiece came from IEEE/The Open Group, dated 2017, and it's filed under FMEMOPEN(3POSIX). go forth and memstream responsibly, ily, bye!! πβ¨π
π¦ where this page comes from
- debian bookworm: manpages-posix-dev 2017a-2 Β· Other
- debian trixie: manpages-posix-dev 2017a-2 Β· Other
- ubuntu jammy: manpages-posix-dev 2017a-2 Β· Other
- ubuntu noble: manpages-posix-dev 2017a-2 Β· Other
- ubuntu resolute: manpages-posix-dev 2017a-2build1 Β· Other
- ubuntu stonking: manpages-posix-dev 2017a-2.1 Β· Other
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms