1<html lang="en"> 2<head> 3<title>fmemopen - Untitled</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="Untitled"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="Stdio.html#Stdio" title="Stdio"> 9<link rel="prev" href="fileno.html#fileno" title="fileno"> 10<link rel="next" href="fopen.html#fopen" title="fopen"> 11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 12<meta http-equiv="Content-Style-Type" content="text/css"> 13<style type="text/css"><!-- 14 pre.display { font-family:inherit } 15 pre.format { font-family:inherit } 16 pre.smalldisplay { font-family:inherit; font-size:smaller } 17 pre.smallformat { font-family:inherit; font-size:smaller } 18 pre.smallexample { font-size:smaller } 19 pre.smalllisp { font-size:smaller } 20 span.sc { font-variant:small-caps } 21 span.roman { font-family:serif; font-weight:normal; } 22 span.sansserif { font-family:sans-serif; font-weight:normal; } 23--></style> 24<link rel="stylesheet" type="text/css" href="../cs.css"> 25</head> 26<body> 27<div class="node"> 28<a name="fmemopen"></a> 29<p> 30Next: <a rel="next" accesskey="n" href="fopen.html#fopen">fopen</a>, 31Previous: <a rel="previous" accesskey="p" href="fileno.html#fileno">fileno</a>, 32Up: <a rel="up" accesskey="u" href="Stdio.html#Stdio">Stdio</a> 33<hr> 34</div> 35 36<h3 class="section">4.16 <code>fmemopen</code>—open a stream around a fixed-length string</h3> 37 38<p><a name="index-fmemopen-177"></a><strong>Synopsis</strong> 39<pre class="example"> #include <stdio.h> 40 FILE *fmemopen(void *restrict <var>buf</var>, size_t <var>size</var>, 41 const char *restrict <var>mode</var>); 42 43</pre> 44 <p><strong>Description</strong><br> 45<code>fmemopen</code> creates a seekable <code>FILE</code> stream that wraps a 46fixed-length buffer of <var>size</var> bytes starting at <var>buf</var>. The stream 47is opened with <var>mode</var> treated as in <code>fopen</code>, where append mode 48starts writing at the first NUL byte. If <var>buf</var> is NULL, then 49<var>size</var> bytes are automatically provided as if by <code>malloc</code>, with 50the initial size of 0, and <var>mode</var> must contain <code>+</code> so that data 51can be read after it is written. 52 53 <p>The stream maintains a current position, which moves according to 54bytes read or written, and which can be one past the end of the array. 55The stream also maintains a current file size, which is never greater 56than <var>size</var>. If <var>mode</var> starts with <code>r</code>, the position starts at 57<code>0</code>, and file size starts at <var>size</var> if <var>buf</var> was provided. If 58<var>mode</var> starts with <code>w</code>, the position and file size start at <code>0</code>, 59and if <var>buf</var> was provided, the first byte is set to NUL. If 60<var>mode</var> starts with <code>a</code>, the position and file size start at the 61location of the first NUL byte, or else <var>size</var> if <var>buf</var> was 62provided. 63 64 <p>When reading, NUL bytes have no significance, and reads cannot exceed 65the current file size. When writing, the file size can increase up to 66<var>size</var> as needed, and NUL bytes may be embedded in the stream (see 67<code>open_memstream</code> for an alternative that automatically enlarges the 68buffer). When the stream is flushed or closed after a write that 69changed the file size, a NUL byte is written at the current position 70if there is still room; if the stream is not also open for reading, a 71NUL byte is additionally written at the last byte of <var>buf</var> when the 72stream has exceeded <var>size</var>, so that a write-only <var>buf</var> is always 73NUL-terminated when the stream is flushed or closed (and the initial 74<var>size</var> should take this into account). It is not possible to seek 75outside the bounds of <var>size</var>. A NUL byte written during a flush is 76restored to its previous value when seeking elsewhere in the string. 77 78 <p><br> 79<strong>Returns</strong><br> 80The return value is an open FILE pointer on success. On error, 81<code>NULL</code> is returned, and <code>errno</code> will be set to EINVAL if <var>size</var> 82is zero or <var>mode</var> is invalid, ENOMEM if <var>buf</var> was NULL and memory 83could not be allocated, or EMFILE if too many streams are already 84open. 85 86 <p><br> 87<strong>Portability</strong><br> 88This function is being added to POSIX 200x, but is not in POSIX 2001. 89 90 <p>Supporting OS subroutines required: <code>sbrk</code>. 91 92 <p><br> 93 94 </body></html> 95 96