c - 使用函数 fprint 和 int 数组重写文件

标签 c arrays

我想使用 fprintf 用 int 数组的内容覆盖一个文件。这应该是一个非常简单的程序,但是,我没有得到预期的输出。请不要建议使用其他函数,因为这是单向赋值,我们必须使用数组和 fprintf。

所以当我期待文件包含:

3 3 3 3 3 3 3 3 3 3 3 3 

我得到:

1495894796 1495894796 1495894796 1495894796 1495894796 1495894796 
1495894796 1495894796 1495894796 1495894796 1495894796 1495894796

还有这个编译器警告:

  format specifies type 'int' but the argument has type 'int *' [-Wformat]

我不知道是什么导致了这个错误,因为我有一个运行良好的非常相似的程序,但我在其中使用了一个字符数组和函数 fputc。

这是我的代码的相关部分,如有必要,请随时索取完整代码:

    #include<stdio.h>
    #include<stdlib.h>

    #define ARRAY_ELEMENTS 13
    /* Defining the size of my int array */

    int main(){
      int i, aux;
      FILE *finformation
      int defined_array[ARRAY_ELEMENTS];

      i = 0;

      while (i < (ARRAY ELEMENTS - 1))
      {
          defined_array[i] = 3;
          i = i+1;
      }
      defined_array[i] = 999;
      /* This should initialize my int array with all the numbers
      being 3 but the last one being 999 */

      finformation = fopen("/Users/(path here)/file.txt", "w");
      /* I'm pretty sure I didn't screw up here because the file does
      get overwritten */

      i = 0;
      aux = defined_array[i];
      /* I want to read the first number in the int array 
      and store it in an int variable: aux. Here is where I've
      probably messed up things */

      while (aux != 999)
      {
          fprintf(finformation,"%d ", &aux);
          i = i+1;
          aux = defined_array[i];
      }
      /* While I don't read the last number in my int array (999), I  
      want to write the last read number into the file. Then, leave
      an space and repeat */

      fclose(finformation);
      return 0;
    }

最佳答案

fprintf 调用中删除 aux 前面的 &

简单使用

fprintf(finformation, "%d ", aux);

& 正在将类型形式 int 更改为 int*(指向整数的指针)。

关于c - 使用函数 fprint 和 int 数组重写文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33391223/

相关文章:

python - 用相同的一维数组插入二维数组的每一行

c - 如何在 Linux 中使用参数执行 C 代码中的外部程序?

c - 列出所有排列的代码的时间复杂度?

c - 将 C 字符串保存到 RAM 时描述内存

php - 将 3 个查询组合成一个数组

php - 以 JSON 格式输出 SQL 结果数组的正确方法

c - 将程序集 'shl' 、 'OR' 、 'AND' 、 'SHR' 操作转换为 C 的引用?

c - OpenMP 矩阵乘法关键部分

javascript - 循环遍历数组并过滤电子邮件

Java 洗牌牌