Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Using C, I would do something like this (untested):</p> <pre><code>#include &lt;stdio.h&gt; #define MAX 128 char buf[MAX]; while (fgets(buf, sizeof buf, fp) != NULL) { double d1, d2; if (buf[0] == '\n') { /* saw blank line */ } else if (sscanf(buf, "%lf%lf", &amp;d1, &amp;d2) != 2) { /* buf has the next text token, including '\n' */ } else { /* use the two doubles, d1, and d2 */ } } </code></pre> <p>The check for blank line is first because it's relatively inexpensive. Depending upon your needs:</p> <ol> <li>you might need to increase/change <code>MAX</code>,</li> <li>you may need to check if <code>buf</code> ends with a newline, if it doesn't, then the line was too long (go to 1 or 3 in that case),</li> <li>you might need a function that reads full lines from a file, using <code>malloc()</code> and <code>realloc()</code> to dynamically allocate the buffer (see <a href="http://www.cpax.org.uk/prg/writings/fgetdata.php" rel="nofollow noreferrer">this</a> for more),</li> <li>you might want to take care of special cases such as a single floating-point value on a line (which I assume is not going to happen). <code>sscanf()</code> returns the number of input items successfully matched and assigned.</li> </ol> <p>I am also assuming that blank lines are really blank (just the newline character by itself). If not, you will need to skip leading white-space. <code>isspace()</code> in <code>ctype.h</code> is useful in that case.</p> <p><code>fp</code> is a valid <code>FILE *</code> object returned by <code>fopen()</code>.</p>
 

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