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

Why binary data file is bigger then data?

我使用二进制文件使用 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);