Go to the previous, next section.
Inside every custom stream is a special object called the cookie.
This is an object supplied by you which records where to fetch or store
the data read or written. It is up to you to define a data type to use
for the cookie. The stream functions in the library never refer
directly to its contents, and they don't even know what the type is;
they record its address with type void *.
To implement a custom stream, you must specify how to fetch or store the data in the specified place. You do this by defining hook functions to read, write, change "file position", and close the stream. All four of these functions will be passed the stream's cookie so they can tell where to fetch or store the data. The library functions don't know what's inside the cookie, but your functions will know.
When you create a custom stream, you must specify the cookie pointer,
and also the four hook functions stored in a structure of type
struct cookie_io_functions.
These facilities are declared in `stdio.h'.
Data Type: struct cookie_io_functions
This is a structure type that holds the functions that define the communications protocol between the stream and its cookie. It has the following members:
cookie_read_function *read
EOF.
cookie_write_function *write
cookie_seek_function *seek
fseek on this stream can only seek to locations within the
buffer; any attempt to seek outside the buffer will return an
ESPIPE error.
cookie_close_function *close
Function: FILE * fopencookie (void *cookie, const char *opentype, struct cookie_functions io_functions)
This function actually creates the stream for communicating with the
cookie using the functions in the io_functions argument.
The opentype argument is interpreted as for fopen;
see section Opening Streams. (But note that the "truncate on
open" option is ignored.) The new stream is fully buffered.
The fopencookie function returns the newly created stream, or a null
pointer in case of an error.
Go to the previous, next section.