Copyright (C) 2000-2012 |
GNU Info (libc.info)Obstack StreamsObstack Streams --------------- You can open an output stream that puts it data in an obstack. Note: Obstacks. - Function: FILE * open_obstack_stream (struct obstack *OBSTACK) 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 (Note: 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 to `fflush', you can examine the object temporarily. You can move the file position of an obstack stream with `fseek' or `fseeko' (Note: 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 use `obstack_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 another `fflush' and `obstack_finish'. But how do you find out how long the object is? You can get the length in bytes by calling `obstack_object_size' (Note: 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); } automatically generated by info2www version 1.2.2.9 |