fmemopen Subroutine

Purpose

Opens a memory buffer stream.

Library

Standard Library (libc.a)

Syntax

#include <stdio.h>
FILE *fmemopen (void *restrict buf, size_t size, const char *restrict mode);

Description

The fmemopen subroutine associates the buffer given by the buf and size arguments with a stream. The buf argument must be either a null pointer or point to a buffer that contains the value specified by the size parameter in bytes.

The mode argument is a character string having one of the following values:
  • r or rb to open the stream for reading.
  • w or wb to open the stream for writing.
  • a or ab Append to pen the stream for writing at the first null byte.
  • r+ or rb+ or r+b to open the stream for update (reading and writing).
  • w+ or wb+ or w+b to open the stream for update (reading and writing). Truncates the buffer contents.
  • a+ or ab+ or a+b Append to open the stream for update (reading and writing) and the initial position is at the first null byte.

The character b does not have any effect.

If a null pointer is specified as the buf argument, the fmemopen subroutine allocates the number of bytes specified by the size parameter to the memory by a call to the malloc subroutine. This buffer is automatically released when the stream is closed. Because this feature is only useful when the stream is opened for updating since there is no way to get a pointer to the buffer, the fmemopen subroutine call fails if the mode argument does not include a + character.

The stream maintains a current position in the buffer. This position is initially set to either the beginning of the buffer (for r and w modes) or to the first null byte in the buffer (for a modes). If no null byte is found in the append mode, the initial position is set to one byte after the end of the buffer.

If buf is a null pointer, the initial position is always set to the beginning of the buffer.

The stream also maintains the size of the current buffer contents. For modes r and r+ the size is set to the value given by the size argument. For modes w and w+ the initial size is zero and for modes a and a+ the initial size is either the position of the first null byte in the buffer or the value of the size argument if no null byte is found.

A read operation on the stream does not advance the current buffer position behind the current buffer size. Reaching the buffer size in a read operation counts as end of file. Null bytes in the buffer have no special meaning for reads. The read operation starts at the current buffer position of the stream.

A write operation starts either at the current position of the stream (if mode does not contain a as the first character) or at the current size of the stream (if mode does not contain 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 is set to the current position. A write operation on the stream doesl not advance the current buffer size behind the size given in the size argument.

When a stream opened for writing is flushed or closed, a null byte is 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 is 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 fails.

Return Values

Upon successful completion, the fmemopen subroutine returns a pointer to the object controlling the stream. Otherwise, a null pointer is returned, and the errno variable is set to indicate the error.

Error Codes

The fmemopen function returns the following error code:
Table 1. Error codes
Item Description
EINVAL The size argument specifies a buffer size of zero or the value of the mode argument is not valid or the buf argument is a null pointer and the mode argument does not include a + character.
EMFILE FOPEN_MAX streams are currently open in the calling process.
ENOMEM The buf argument is a null pointer and the allocation of a buffer of length specified by the size parameter has failed.

Examples

#include <stdio.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);
} 

The above program produces the following output:

Got f

Got o

Got o

Got b

Got a

Got r