You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
5.8 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 6.4, http://www.gnu.org/software/texinfo/ -->
<head>
<title>open_memstream (The Red Hat newlib C Library)</title>
<meta name="description" content="open_memstream (The Red Hat newlib C Library)">
<meta name="keywords" content="open_memstream (The Red Hat newlib C Library)">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Document-Index.html#Document-Index" rel="index" title="Document Index">
<link href="Document-Index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Stdio.html#Stdio" rel="up" title="Stdio">
<link href="perror.html#perror" rel="next" title="perror">
<link href="mktemp.html#mktemp" rel="prev" title="mktemp">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smalllisp {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
-->
</style>
</head>
<body lang="en">
<a name="open_005fmemstream"></a>
<div class="header">
<p>
Next: <a href="perror.html#perror" accesskey="n" rel="next">perror</a>, Previous: <a href="mktemp.html#mktemp" accesskey="p" rel="prev">mktemp</a>, Up: <a href="Stdio.html#Stdio" accesskey="u" rel="up">Stdio</a> &nbsp; [<a href="Document-Index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Document-Index.html#Document-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="open_005fmemstream_002c-open_005fwmemstream_002d_002d_002dopen-a-write-stream-around-an-arbitrary_002dlength-string"></a>
<h3 class="section">4.43 <code>open_memstream</code>, <code>open_wmemstream</code>&mdash;open a write stream around an arbitrary-length string</h3>
<a name="index-open_005fmemstream"></a>
<a name="index-open_005fwmemstream"></a>
<p><strong>Synopsis</strong>
</p><div class="example">
<pre class="example">#include &lt;stdio.h&gt;
FILE *open_memstream(char **restrict <var>buf</var>,
size_t *restrict <var>size</var>);
#include &lt;wchar.h&gt;
FILE *open_wmemstream(wchar_t **restrict <var>buf</var>,
size_t *restrict <var>size</var>);
</pre></div>
<p><strong>Description</strong><br>
<code>open_memstream</code> creates a seekable, byte-oriented <code>FILE</code> stream that
wraps an arbitrary-length buffer, created as if by <code>malloc</code>. The current
contents of *<var>buf</var> are ignored; this implementation uses *<var>size</var>
as a hint of the maximum size expected, but does not fail if the hint
was wrong. The parameters <var>buf</var> and <var>size</var> are later stored
through following any call to <code>fflush</code> or <code>fclose</code>, set to the
current address and usable size of the allocated string; although
after fflush, the pointer is only valid until another stream operation
that results in a write. Behavior is undefined if the user alters
either *<var>buf</var> or *<var>size</var> prior to <code>fclose</code>.
</p>
<p><code>open_wmemstream</code> is like <code>open_memstream</code> just with the associated
stream being wide-oriented. The size set in <var>size</var> in subsequent
operations is the number of wide characters.
</p>
<p>The stream is write-only, since the user can directly read *<var>buf</var>
after a flush; see <code>fmemopen</code> for a way to wrap a string with a
readable stream. The user is responsible for calling <code>free</code> on
the final *<var>buf</var> after <code>fclose</code>.
</p>
<p>Any time the stream is flushed, a NUL byte is written at the current
position (but is not counted in the buffer length), so that the string
is always NUL-terminated after at most *<var>size</var> bytes (or wide characters
in case of <code>open_wmemstream</code>). However, data previously written beyond
the current stream offset is not lost, and the NUL value written during a
flush is restored to its previous value when seeking elsewhere in the string.
</p>
<br>
<p><strong>Returns</strong><br>
The return value is an open FILE pointer on success. On error,
<code>NULL</code> is returned, and <code>errno</code> will be set to EINVAL if <var>buf</var>
or <var>size</var> is NULL, ENOMEM if memory could not be allocated, or
EMFILE if too many streams are already open.
</p>
<br>
<p><strong>Portability</strong><br>
POSIX.1-2008
</p>
<p>Supporting OS subroutines required: <code>sbrk</code>.
</p>
<br>
<hr>
<div class="header">
<p>
Next: <a href="perror.html#perror" accesskey="n" rel="next">perror</a>, Previous: <a href="mktemp.html#mktemp" accesskey="p" rel="prev">mktemp</a>, Up: <a href="Stdio.html#Stdio" accesskey="u" rel="up">Stdio</a> &nbsp; [<a href="Document-Index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Document-Index.html#Document-Index" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>