【问题标题】:C: Cannot get fprintf to print to output-file [closed]C:无法让 fprintf 打印到输出文件 [关闭]
【发布时间】:2016-06-02 04:15:29
【问题描述】:

我正在尝试将矩阵打印到 csv 中。其中的 arg[2] 是文件名,我可以验证它是否正常工作,因为它会生成文件但不会填充它。我确实关闭了文件并尝试刷新它,但它不起作用。

// Open the output/second file and write the contents of truncated DCT matrix into it
outputfp = fopen(argv[2], "w");

if (outputfp == NULL) {

fprintf(stderr, "Can't open output file %s!\n", argv[2]);
exit(1);
                      }
double hold = 0;
printf("test\n");

for (i = 0, i < idx; i++;) {
    for (j = 0, j < ARRAY_WIDTH; j++;) {

    hold = test_write[i][j];

            fprintf(outputfp, "%.61f", hold);
                if (j != ARRAY_WIDTH) {

                    fprintf(outputfp, ",");
                }
                else {
                    //continue;
                }
                fflush(outputfp);
    }
}

fclose (outputfp);
return 0;
}

【问题讨论】:

    标签: c printf fopen


    【解决方案1】:

    这个循环

     for (j = 0, j < ARRAY_WIDTH; j++;) {
    

    从不迭代。

    ,; 的错误放置使 j++ 成为迭代条件。由于在第一次迭代之前j++ 的计算结果为0,因此永远不会进入循环。显然,你的意思是写

     for (j = 0; j < ARRAY_WIDTH; j++) {
    

    【讨论】:

    • 谢谢我之前遇到了一个问题,虽然我已经摆脱了所有循环,但我错误地输入了 for 循环。我只需要第二双眼睛。我已经把它修好了并且可以工作了。我只是很困惑为什么编译器将我拥有的内容视为有效的 c 代码。
    猜你喜欢
    • 2017-01-20
    • 1970-01-01
    • 1970-01-01
    • 2013-04-19
    • 1970-01-01
    • 1970-01-01
    • 2012-01-29
    • 2016-06-12
    • 1970-01-01
    相关资源
    最近更新 更多