c - fwrite => fprintf

标签 c

我在将 fwrite(tmp_array, sizeof(int), num, f); 行更改为 fprintf 时遇到问题。

有人可以帮我看看吗?

 void generate_random_sorted_file(const char *file_name, int num)
 {
     FILE *f = fopen(file_name, "wb");
     if (f==NULL) 
     {
         printf("could not open %s\n", file_name);
         return;
     }

     int *tmp_array = calloc(num, sizeof(int));
     int i;

     for (i=0; i<num; i++)
     tmp_array[i]=rand();

     qsort (tmp_array, num, sizeof(int), compare); /* sorts the array */
     fwrite(tmp_array, sizeof(int), num, f);

     fclose(f);
 }

最佳答案

fprintf 会将您的整数数组写成文本,如果这是您想要的,请执行类似的操作

int i;
for(i = 0; i < num; i++)
  fprintf(f,"%d ",tmp_array[i]);

关于c - fwrite => fprintf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6426413/

相关文章:

c - Visual Studio 内联汇编程序未按预期工作

c - 为什么我可以相信内存分配?

c - 如何在 C 中的链表中实现数组

无法理解 C 中的 unsigned int 类型

c - 具有共享内存和进程的生产者/消费者

c - scanf() 之后跳过输入

c - 如何更改 C 函数中的指针值

c - 递归仅在使用断点或 getchar 时有效

c - 如何从字符串中提取实数?

C: feof 的奇怪行为