Hello Hartmut,
gr_stream_init_file takes an existing file pointer as input. Currently there is no method to retrieve the file pointer from the stream object, so you will either have to reach into the gr_stream_struct yourself or store a copy of the file pointer externally until you've finished writing to the stream. So the usage should be something like this:
FILE * fp = fopen(...);
gr_stream_init_file(stream, fp);
gr_write(stream, x, ctx);
gr_write(stream, y, ctx);
gr_write(stream, z, ctx);
fclose(fp);
/* At this point, stream can no longer be used */
For a stream created with gr_stream_init_str, the string is allocated internally and may be resized internally. You can retrieve the current pointer with gr_get_str. Once you have done so, you should not do any further writes to the same stream object since this could invalidate the pointer. Retrieving and freeing the string is basically how you clear such a stream object.
gr_stream_init_str(stream);
gr_write(stream, x, ctx);
gr_write(stream, y, ctx);
gr_write(stream, z, ctx);
s = gr_get_str(stream);
flint_printf("%s\n", s);
flint_free(s);
/* At this point, stream can no longer be used */
gr_stream_write_si, gr_stream_write_ui and gr_stream_write_fmpz don't write directly to the stream; they write to a temporary string and then copy that string into the stream. For gr_stream_write_fmpz this temporary string is malloced and thus needs to be freed. This is an implementation detail which you normally shouldn't need to be concerned with externally.
Fredrik