c - 为什么二进制数据文件比数据大?

标签 c arrays gcc binaryfiles fwrite

我使用二进制文件使用 C 保存和读取一维数据数组。这是 int 类型的代码:

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

int main ()
{
  FILE * pbFile;

  char * fName  = "myfile.bin";

  // input static array
  int a[] = { 1 , 2 , 3 };
  long ALength; // number of array's elements
  size_t ASize; // size of array in bytes
  size_t ESize; // size of array's element in bytes
  int i;

  // for output array (dynamic arrray read from binary file )
  int * buffer; // 
  size_t result;

  size_t FSize;




  // test input
  ASize = sizeof(a); // 
  ESize = sizeof(a[0]);
  ALength = ASize/ESize; // number of elements  = Length of array


  printf("size of int = %zu\n", ESize);
  printf("array has %ld elements and size = %ld * %zu =  %zu bytes\n", ALength, ALength, ESize,  ASize ); 
  for (i=0; i< ALength; i++)
    printf("i = %d \t a[i] = %d\n",i, a[i]);


  // write to the binary file 
  pbFile = fopen (fName, "wb");
  fwrite (a , ESize, ASize, pbFile);
  fclose (pbFile);
  printf("array have been saved to the  %s file\n", fName);

  // open binary file 
  pbFile = fopen ( fName , "rb" );
  if (pbFile==NULL) {fputs ("File open error",stderr); return 1;}

   // obtain file size:
  fseek (pbFile , 0 , SEEK_END);
  FSize = ftell (pbFile);
  rewind (pbFile);
   printf("file size = %zu\n", FSize);

  // allocate memory to contain the whole file:
  buffer = (int*) malloc (ESize*ALength);
  if (buffer == NULL) {fputs ("Memory error",stderr); return 2;}

  // copy the file into the buffer:
  result = fread (buffer,ESize,ALength,pbFile);
  if (result != ALength) {fputs ("Reading error",stderr); return 3;}

  /* the whole file is now loaded in the memory buffer. */

  // print the array 
  printf("array has %ld elements and size of %zu bytes\n", ALength, ASize ); 
  for (i=0; i< ALength; i++)
    printf("i = %d \t a[i] = %d\n",i, buffer[i]);


  // terminate
  fclose (pbFile);
  free (buffer);


  return 0;
}

这是输出:

 gcc a.c -Wall
 ./a.out


size of int = 4
array has 3 elements and size = 3 * 4 =  12 bytes
i = 0    a[i] = 1
i = 1    a[i] = 2
i = 2    a[i] = 3
array have been saved to the  myfile.bin file
file size = 48
array has 3 elements and size of 12 bytes
i = 0    a[i] = 1
i = 1    a[i] = 2
i = 2    a[i] = 3

我也检查了文件:

hexdump -d myfile.bin
0000000   00001   00000   00002   00000   00003   00000   00512   62599
0000010   52238   25551   11312   60002   21907   00000   27543   60114
0000020   32669   00000   00001   00000   00000   00000   00360   48936
0000030


hexdump -C myfile.bin
00000000  01 00 00 00 02 00 00 00  03 00 00 00 00 7d ea 89  |.............}..|
00000010  c4 dc 93 ae 60 dc fd 33  c7 55 00 00 97 fb 5f 03  |....`..3.U...._.|
00000020  fa 7f 00 00 01 00 00 00  00 00 00 00 a8 a2 b3 22  |..............."|
00000030

文件和数组大小取决于元素数量和元素大小。

当数据为 char 时,文件大小等于数组大小(以字节为单位),但对于 int 和 double 类型,它更大。

为什么以字节为单位的文件大小大于数据大小?

最佳答案

您向 fwrite 传递了错误的参数。您应该使用元素的数量,而不是第三个参数的数组大小。

fwrite (a, ESize, ALength, pbFile);

关于c - 为什么二进制数据文件比数据大?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58564758/

相关文章:

c - 为什么值超出了数组的索引?

c++ - 包括 tr1::shared_ptr

c - 为什么在 Linux 编译器中编译和运行 C 程序时需要额外的参数?

c - C解释中的运算符优先级

arrays - 使用复选框和 AngularJS 过滤 ng-repeat 列表

javascript - 将 JSON 数组转换为单独的 JS 变量

c++ - 为 gcov 编译 googletest

c - 如何在 C 或 ASM 中获取调用函数的传入参数

c - IPC 使用共享内存

C - 动态字符串释放和动态数组