Note that there are some explanatory texts on larger screens.

plurals
  1. POFlexible, compatible alternative to FILE / fopen I/O
    text
    copied!<p>I have a bunch of legacy code that uses C-style file I/O (using <code>FILE*</code> streams, <code>fprintf</code>, <code>fscanf</code>, etc.) that I'd like to update to operate off of other data sources (e.g., downloading from or posting to the web, or to or from a custom data transfer cable).</p> <p>If it used iostreams, this would be an easy update, but it doesn't.</p> <p>Are there any C or C++ libraries for I/O that support different kinds of streams but that are source-compatible or mostly source-compatible with C's <code>FILE*</code> stream functions?</p> <p>If source compatibility is too much to ask, I'd settle for "able to be search-and-replaced with a Perl script." <code>fprintf</code> and <code>fscanf</code> seem to be the major obstacles here; I haven't found any <code>iostream</code>-compatible versions of these.</p> <p>I'm looking for a solution that will work in both Windows and Linux.</p> <p><strong>Example</strong></p> <p>As a trivial example, I have code like the following:</p> <pre><code>FILE *input = fopen(filename, "rt"); fscanf(input, "%d,%d,%d,%f", &amp;f_id, &amp;f_units, &amp;h_index, &amp;h_default); fclose(input); </code></pre> <p>I'd like to update it to handle data that's of the same format but may come from other sources, without having to worry about the intricacies of how exactly to replicate <code>fscanf</code>'s semantics myself (since I have many, many <code>fscanf</code> and <code>fprintf</code> calls that would need updating). Something like this would work, except that I've been unable to find anything like <code>istream_scanf</code>:</p> <pre><code>std::istream *input = make_http_istream(url); istream_scanf(*input, &amp;f_id, &amp;f_units, &amp;h_index, &amp;h_default); delete input; </code></pre>
 

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