Note that there are some explanatory texts on larger screens.

plurals
  1. POIn what scenarios would fprintf() not write to the output file
    text
    copied!<p>This is a sub-problem of a bigger problem I have posted before. Following is a code snippet from a C++ package I am trying to work with. The intent is to write the values in the float array prob_estimates to the output file. For some seemingly random lines, only some of the values of the array are written. When can that happen? How should I debug it?</p> <pre><code> int j; predict_label = predict_probability(model_,x,prob_estimates); fprintf(output,"%g",predict_label); for(j=0;j&lt;model_-&gt;nr_class;j++) { fprintf(output," %g",prob_estimates[j]); fflush(output); } fprintf(output,"\n"); </code></pre> <p>I also want to point out that this seems to happen only when the input size is fairly huge. This is a part of a bigger loop which runs per line of an input file (with about 200,000 lines). The prob_estimates array has 500 values per line. The output file writes less than 500 values for some 20-odd lines in the output file. I ran this a couple of times on a subset (with 20,000 lines) and everything seemed fine.</p> <p>Update: I tried checking the return value of fprintf after each attempted write and turns out it returns -1 for a lot of lines, when trying to write to the output.</p> <pre><code>fprintf encountered error at 19th value of line 2109359. Returned -1 fprintf encountered error at 373th value of line 2109359. Returned -1 fprintf encountered error at 229th value of line 2109360. Returned -1 fprintf encountered error at 87th value of line 2109361. Returned -1 fprintf encountered error at 439th value of line 2109361. Returned -1 </code></pre> <p>This is when I modified the above code as follows:</p> <pre><code> for(j=0;j&lt;model_-&gt;nr_class;j++) { int e = fprintf(output," %g",prob_estimates[j]); if (e &lt; 0) { printf("fprintf encountered error at %dth value of line %d. Returned %d",j ,count ,e); } } </code></pre> <p>Here <code>count</code> is a variable that counts the number of line. It is incremented at the top of the outer loop (not shown here). What can I do to figure out why fprintf returns -1?</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