Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>above is my c code for reading a table from a file.In that ..last value is printing 2 times !!!</p> </blockquote> <p>The last value is printing two times due to the structure of the file reading loop. The <code>eof()</code> flag is not set until an attempt is made to read past the end of the file. When <code>fscanf()</code> reads the last two <code>long</code>s from the last line of the file <code>eof()</code> is not yet set but the next call to <code>fscanf()</code> fails and sets <code>eof()</code> but the result of <code>fscanf()</code> is not queried immediately, resulting the use of the previously extracted <code>long</code>s: check the result of all read operations immediately.</p> <hr> <p>A possible solution is to read a line at a time, using <a href="http://en.cppreference.com/w/c/io/fgets" rel="nofollow"><code>fgets()</code></a>, and then use <a href="http://en.cppreference.com/w/c/io/fscanf" rel="nofollow"><code>sscanf()</code></a> to extract the <code>long</code> value(s) from the read line. If <code>fscanf()</code> is used, it would read past the new-line character to locate the second requested <code>long</code>, which is not the desired behaviour.</p> <p>For example:</p> <pre><code>char line[1024]; while (fgets(line, 1024, fin)) { /* Assign appropriate default values. sscanf() does not modify its arguments for which it has no value to assign. So if 'line' has a single long value data[1] will be zero. */ long data[2] = { 0, 0 }; /* You can use 'result' if you require to take particular action if it reads only 1, or 0, items. */ int result = sscanf(line, "%ld %ld", &amp;data[0], &amp;data[1]); printf("\n%ld %ld",data[0],data[1]); } </code></pre> <hr> <p>(<em>in response to question update</em>) To differentiate between lines where second value is missing:</p> <blockquote> <p>2</p> </blockquote> <p>and lines where first value is missing:</p> <blockquote> <p>57</p> </blockquote> <p>a valid range (or some other criteria) is required to determine which value (the first or second) was missing from the line:</p> <pre><code>int result = sscanf(line, "%ld %ld", &amp;data[0], &amp;data[1]); if (1 == result) { if (data[0] &gt;= 1 &amp;&amp; data[0] &lt;= 9) { printf("\n%ld 0", data[0]); } else { /* Read value was the second value. */ printf("\n%ld %ld", ++last_first_value, data[0]); } } </code></pre> <p>where <code>last_first_value</code> is a <code>long</code> that stores the current value of the first value (either the last successfully read first value or computed from the last successfully read first value). </p>
    singulars
    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.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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