Next: Custom Streams, Previous: String Streams, Up: Other Kinds of Streams
You can open an output stream that puts it data in an obstack. See Obstacks.
This function opens a stream for writing data into the obstack obstack. This starts an object in the obstack and makes it grow as data is written (see Growing Objects).
Calling
fflush
on this stream updates the current size of the object to match the amount of data that has been written. After a call tofflush
, you can examine the object temporarily.You can move the file position of an obstack stream with
fseek
orfseeko
(see File Positioning). Moving the file position past the end of the data written fills the intervening space with zeros.To make the object permanent, update the obstack with
fflush
, and then useobstack_finish
to finalize the object and get its address. The following write to the stream starts a new object in the obstack, and later writes add to that object until you do anotherfflush
andobstack_finish
.But how do you find out how long the object is? You can get the length in bytes by calling
obstack_object_size
(see Status of an Obstack), or you can null-terminate the object like this:obstack_1grow (obstack, 0);Whichever one you do, you must do it before calling
obstack_finish
. (You can do both if you wish.)
Here is a sample function that uses open_obstack_stream
:
char * make_message_string (const char *a, int b) { FILE *stream = open_obstack_stream (&message_obstack); output_task (stream); fprintf (stream, ": "); fprintf (stream, a, b); fprintf (stream, "\n"); fclose (stream); obstack_1grow (&message_obstack, 0); return obstack_finish (&message_obstack); }