【发布时间】:2019-10-25 19:38:59
【问题描述】:
我使用二进制文件使用 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 类型,它更大。
为什么文件的字节大小比数据大小大?
【问题讨论】:
-
-d不是双精度类型,它是 十进制。我真的看不出你有什么不同 -
@Eugene Sh.你的意思是 d hexdump 选项?区别:文件大小为 48,数据为 12。为什么文件大小不等于数据大小。如果是 3 个双数数组有 24 个字节,文件有 192 个字节。
-
是的。但我可能不理解你的问题,但我没有看到问题中有任何重复,但你问他们。
-
你打电话给
fwrite的尺寸有误。你想要fwrite (a, ESize, Alength, pbFile);。
标签: c arrays gcc binaryfiles fwrite