---
name: "open_wmemstream"
section: "3"
platform: "linux"
package: "man-pages"
version: "6.19-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux", "freebsd"]
url: "https://new.carta.tech/open_memstream"
---

# open\_wmemstream(3)

open a dynamic memory buffer stream

Part of [open\_memstream(3)](/open_memstream/3.md).

## NAME

open\_memstream, open\_wmemstream - open a dynamic memory buffer stream

## LIBRARY

Standard C library (*libc*, *-lc*)

## 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)](/feature_test_macros/7.md)):

**open\_memstream**(), **open\_wmemstream**():

```
    Since glibc 2.10:
        _POSIX_C_SOURCE >= 200809L
    Before glibc 2.10:
        _GNU_SOURCE
```

## DESCRIPTION

The **open\_memstream**() function opens a stream for writing to a memory buffer. The function dynamically allocates the buffer, and the buffer automatically grows as needed. Initially, the buffer has a size of zero. After closing the stream, the caller should [**free**(3)](/free/3.md) this buffer.

The locations pointed to by *ptr* and *sizeloc* are used to report, respectively, the current location and the size of the buffer. The locations referred to by these pointers are updated each time the stream is flushed ([**fflush**(3)](/fflush/3.md)) and when the stream is closed ([**fclose**(3)](/fclose/3.md)). These values remain valid only as long as the caller performs no further output on the stream. If further output is performed, then the stream must again be flushed before trying to access these values.

A null byte is maintained at the end of the buffer. This 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)](/fseek/3.md) or [**fseeko**(3)](/fseeko/3.md). Moving the buffer position past the end of the data already written fills the intervening space with null characters.

The **open\_wmemstream**() is similar to **open\_memstream**(), but operates on wide characters instead of bytes.

## 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.

## ATTRIBUTES

For an explanation of the terms used in this section, see [**attributes**(7)](/attributes/7.md).

| | | |
| --- | --- | --- |
| **Interface** | **Attribute** | **Value** |
| **open\_memstream**(), **open\_wmemstream**() | Thread safety | MT-Safe |

## STANDARDS

POSIX.1-2008.

## HISTORY

- **open\_memstream**()

  glibc 1.0.x.

- **open\_wmemstream**()

  glibc 2.4.

## NOTES

There is no file descriptor associated with the file stream returned by these functions (i.e., [**fileno**(3)](/fileno/3.md) will return an error if called on the returned stream).

## 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)](/fseek/3.md) call fails, returning -1.

## EXAMPLES

See [**fmemopen**(3)](/fmemopen/3.md).

## SEE ALSO

[**fmemopen**(3)](/fmemopen/3.md), [**fopen**(3)](/fopen/3.md), [**setbuf**(3)](/setbuf/3.md)
