【问题标题】:warning: passing argument 1 of 'fprintf' from incompatible pointer type警告:从不兼容的指针类型传递“fprintf”的参数 1
【发布时间】:2020-07-09 00:45:03
【问题描述】:
warning: passing argument 1 of 'fprintf' from incompatible pointer type 
warning: passing argument 2 of 'fprintf' makes pointer from integer without a cast

如何解决这两个警告?我无法生成具有预期输出的文件。

int main(int argc, char* argv[])
{
    int num_values = strtoul(argv[1], NULL, 10);
    value_t* pValues = generate_sequence(num_values);

    randomize_sequence(pValues, num_values);

    // Record results
    //FILE *fd = fopen("/results.txt", "w+");
    for (int i = 0; i < num_values; i++) { //change made: i++ to allow looping
        fprintf("results.txt", i, pValues[i]); //changes made: "fprintf". i and fd were added to the argument
    }
    //fclose(fd); //change made: "fclose"

    return EXIT_SUCCESS;
}

【问题讨论】:

  • fprintf -> printf
  • 您需要将 fd 传递给 fprintf 而不是文件名,或者按照 S.M. 的建议使用 printf
  • fprintf 没有文件名。建议你阅读manual page,通过基本的C教程并通过搜索进行基础研究。
  • 另见stackoverflow.com/q/62804356/2410359 我怀疑在此之后会收到第三篇文章。

标签: c file


【解决方案1】:

fprintf 的第一个参数应该是调用fopen 返回的FILE*

第二个参数是一个格式字符串。任何进一步的参数都插入到格式字符串中的占位符中。

考虑阅读manual page 以更好地理解标准库函数。

【讨论】:

    【解决方案2】:

    fprintf() 的一般语法是

    int fprintf(FILE *stream, const char *format, ...)

    第一个参数FILE *stream 需要一个文件指针,应该使用它来打开操作

    FILE *fptr = fopen("results.txt", "w");

    功能。

    所以,请取消注释行号。 54 和 58 然后在循环内部将 fprintf("results.txt", i, pValues[i]) 更改为 fprintf(fd, "%d", pValues[i])

    还可以了解Basics of File Handling in C

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-10
      • 2020-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多