简体   繁体   English

在什么情况下fprintf()不会写入输出文件

[英]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. 以下是我尝试使用的C ++软件包的代码片段。 The intent is to write the values in the float array prob_estimates to the output file. 目的是将float数组prob_estimates中的值写入输出文件。 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). 这是一个更大的循环的一部分,该循环在输入文件的每行(约200,000行)中运行。 The prob_estimates array has 500 values per line. prob_estimates数组每行有500个值。 The output file writes less than 500 values for some 20-odd lines in the output file. 输出文件为输出文件中的约20余行写了少于500个值。 I ran this a couple of times on a subset (with 20,000 lines) and everything seemed fine. 我在一个子集(有20,000行)上运行了几次,一切似乎都很好。

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的返回值,结果发现在尝试写输出时,它返回-1表示很多行。

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. 这里count是一个对行数进行计数的变量。 It is incremented at the top of the outer loop (not shown here). 它在外循环的顶部递增(此处未显示)。 What can I do to figure out why fprintf returns -1? 我该怎么办才能找出fprintf为什么返回-1?

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 打印model_->nr_class以确保值的数量是您期望的

  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. fclose 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 希望这可以帮助

现在,您已经发现fprintf返回了错误,您需要在失败的调用之后检查errno的值,以找出真正的错误原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM