Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a FILE * stream that results in a string
    primarykey
    data
    text
    <p>I'm looking for a way to pass in a <code>FILE *</code> to some function so that the function can write to it with <code>fprintf</code>. This is easy if I want the output to turn up in an actual file on disk, say. But what I'd like instead is to get all the output as a string (<code>char *</code>). The kind of API I'd like is:</p> <pre><code>/** Create a FILE object that will direct writes into an in-memory buffer. */ FILE *open_string_buffer(void); /** Get the combined string contents of a FILE created with open_string_buffer (result will be allocated using malloc). */ char *get_string_buffer(FILE *buf); /* Sample usage. */ FILE *buf; buf = open_string_buffer(); do_some_stuff(buf); /* do_some_stuff will use fprintf to write to buf */ char *str = get_string_buffer(buf); fclose(buf); free(str); </code></pre> <p>The <strong>glibc</strong> headers seem to indicate that a FILE can be set up with hook functions to perform the actual reading and writing. In my case I think I want the write hook to append a copy of the string to a linked list, and for there to be a <code>get_string_buffer</code> function that figures out the total length of the list, allocates memory for it, and then copies each item into it in the correct place.</p> <p>I'm aiming for something that can be passed to a function such as <code>do_some_stuff</code> without that function needing to know anything other than that it's got a <code>FILE *</code> it can write to.</p> <p>Is there an existing implementation of something like this? It seems like a useful and C-friendly thing to do -- assuming I'm right about the <code>FILE</code> extensibility.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload