atlantis
atlantis

Reputation: 3126

In what scenarios would fprintf() not write to the output file

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?

    int j;
    predict_label = predict_probability(model_,x,prob_estimates);
    fprintf(output,"%g",predict_label);
    for(j=0;j<model_->nr_class;j++) {
        fprintf(output," %g",prob_estimates[j]);
        fflush(output);
    }
    fprintf(output,"\n");

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.

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.

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

This is when I modified the above code as follows:

 for(j=0;j<model_->nr_class;j++) {
            int e = fprintf(output," %g",prob_estimates[j]);
            if (e < 0) {
            printf("fprintf encountered error at %dth value of line %d. Returned %d",j ,count ,e); }
        }

Here count 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?

Upvotes: 0

Views: 2063

Answers (2)

DaveK
DaveK

Reputation: 26

Now you've found that fprintf is returning an error, you need to check the value of errno after the failing call to find out what the actual error cause is.

Upvotes: 0

Israel Unterman
Israel Unterman

Reputation: 13510

A few things you could do:

  1. print everything also to console, to see if the problem is in file output or in another place

  2. print model_->nr_class to make sure the number of values is what you expect

  3. Check the output file only after it is closed. Although you fflush output, it might be that other places update the file and don't fflush it. fclose would. I suggest that instead of flushing the file each line, open it in append mode in the beginning of the function, and close it in the end.

Hope this helps

Upvotes: 1

Related Questions