Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Multiple problems.</p> <p>Firstly, you need to check if inptr and outptr are valid by testing against NULL.</p> <p>Secondly, fscanf can return either EOF, 0 or > 0.</p> <p>If your input file doesn't contain valid input. </p> <p>Also there are problems in that you can get 3 ints read successfull, or 2 ints or 1 and the value of a, b and c are only optionally set.</p> <p>If no conversion took place on the input then the value of zero is returned in which case the while loop will exit.</p> <p>Also bear in mind that with the scanf style functions this input will succeed and return the value of 1.</p> <blockquote> <p>"1rubbish"</p> </blockquote> <p>I think what you may want is something like the following:</p> <pre><code>// Somewhere near the top #include &lt;stderr.h&gt; // ... other includes const char* inname = "trianglein.txt"; const char* outname = "triangleout.txt"; // Any other stuff // Inside main... // Initialization of pointer and opening of file trianglein.txt if ((inptr = fopen(inname,"r")) == 0){ fprintf(stderr, "Error opening file %s: %s", inname, strerror(inname)); return -1; } // Initialization of pointer and opening of file triangleout.txt if ((outptr = fopen(outname,"w")) == 0){ fprintf(stderr, "Error opening file %s: %s", outname, strerror(outname)); return -1; } int result; while(true){ result = fscanf(inptr,"%d %d %d",&amp;a, &amp;b, &amp;c); if (result == EOF) break; if (result &lt; 3) // Ignore incomplete lines continue; // do the normal stuff } </code></pre>
    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.
    3. 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