Quick ?s
Cheat Sheets
Man Pages
The Lynx
Software
FMEMOPEN(3)		   Linux Programmers Manual		  FMEMOPEN(3)



NAME
       fmemopen, open_memstream -  open memory as stream

SYNOPSIS
       #define _GNU_SOURCE
       #include 

       FILE *fmemopen(void *buf, size_t size, const char *mode);

       FILE *open_memstream(char **ptr, size_t *sizeloc);

DESCRIPTION
       The  fmemopen()	function opens a stream that permits the access speci
       fied by mode.  The stream allows I/O to be performed on the  string  or
       memory  buffer  pointed	to  by buf.  This buffer must be at least size
       bytes long.

       The argument mode is the same as for fopen(3).  If  mode  specifies  an
       append  mode,  then the initial file position is set to location of the
       first null byte ('\0') in the buffer; otherwise the initial file  posi
       tion is set to the start of the buffer.

       When  a	stream that has been opened for writing is flushed (fflush(3))
       or closed (fclose(3)), a null byte is written at the end of the	buffer
       if  there  is  space.   The  caller should ensure that an extra byte is
       available in the buffer (and that size counts that byte) to  allow  for
       this.

       Attempts  to  write  more  than	size  bytes to the buffer result in an
       error.  (By default, such errors will only be visible  when  the  stdio
       buffer  is  flushed.   Disabling buffering with setbuf(fp, NULL) may be
       useful to detect errors at the time of an output  operation.   Alterna
       tively,	the  caller can explicitly set buf as the stdio stream buffer,
       at the same time informing stdio  of  the  buffers  size,  using  set
       buffer(fp, buf, size).)

       In  a stream opened for reading, null bytes ('\0') in the buffer do not
       cause read operations to return an end-of-file indication.  A read from
       the  buffer  will  only	indicate  end-of-file  when  the  file pointer
       advances size bytes past the start of the buffer.

       If buf is specified as NULL, then fmemopen()  dynamically  allocates  a
       buffer  size  bytes long.  This is useful for an application that wants
       to write data to a temporary buffer and then read it back  again.   The
       buffer is automatically freed when the stream is closed.  Note that the
       caller has no way to obtain a pointer to the temporary buffer allocated
       by this call (but see open_memstream() below).

       The  open_memstream()  function opens a stream for writing to a buffer.
       The buffer is dynamically allocated (as with malloc(3)), and  automati
       cally  grows  as required.  After closing the stream, the caller should
       free(3) this buffer.

       When the stream is closed (fclose(3)) or flushed (fflush(3)), the loca
       tions  pointed  to  by  ptr and sizeloc are updated to contain, respec
       tively, a pointer to the buffer and the current	size  of  the  buffer.
       These  values  remain valid only as long as the caller performs no fur
       ther output on the stream.  If further output is  performed,  then  the
       stream must again be flushed before trying to access these variables.

       A  null	byte is maintained at the end of the buffer.  This byte is not
       included in the size value stored at sizeloc.

RETURN VALUE
       Upon successful completion fmemopen()  and  open_memstream()  return  a
       FILE  pointer.	Otherwise,  NULL  is  returned and the global variable
       errno is set to indicate the error.

CONFORMING TO
       These functions are GNU extensions.

EXAMPLE
       The program  below  uses  fmemopen()  to  open  an  input  buffer,  and
       open_memstream()  to  open a dynamically sized output buffer.  The pro
       gram scans its input string (taken from the  programs  first  command-
       line  argument) reading integers, and writes the squares of these inte
       gers to the output buffer.  An example of the output produced  by  this
       program is the following:

       $ ./a.out "1 23 43"
       size=11; ptr=1 529 1849

       #define _GNU_SOURCE
       #include 
       #include 
       #include 
       #include 

       #define handle_error(msg) \
	   do { perror(msg); exit(EXIT_FAILURE); } while (0)

       int
       main(int argc, char *argv[])
       {
	   FILE *out, *in;
	   int v, s;
	   size_t size;
	   char *ptr;

	   assert(argc == 2);

	   in = fmemopen(argv[1], strlen(argv[1]), "r");
	   if (in == NULL)
	       handle_error("fmemopen");

	   out = open_memstream(&ptr, &size);
	   if (out == NULL)
	       handle_error("fmemopen");

	   for (;;) {
	       s = fscanf(in, "%d", &v);
	       if (s <= 0)
		   break;

	       s = fprintf(out, "%d ", v * v);
	       if (s == -1)
		   handle_error("fprintf");
	   }
	   fclose(in);
	   fclose(out);
	   printf("size=%ld; ptr=%s\n", (long) size, ptr);
	   free(ptr);
	   exit(EXIT_SUCCESS);
       }

SEE ALSO
       fopen(3), feature_test_macros(7)

COLOPHON
       This  page  is  part of release 3.05 of the Linux man-pages project.  A
       description of the project, and information about reporting  bugs,  can
       be found at http://www.kernel.org/doc/man-pages/.



GNU				  2005-12-08			   FMEMOPEN(3)




Yals.net is © 1999-2009 Crescendo Communications
Sharing tech info on the web for more than a decade!
This page was generated Thu Apr 30 17:05:26 2009