如何有效地将多头数组保存到自定义文件中
How to efficiently save an array of longs to a custom file
我有一个长值数组,我想将它们保存到名为“Longs.myData”的文件中。我希望我的文件大小为 24 个字节,因为数组只有 3 个索引。但是,我得到一个 192 字节的文件。有没有办法保存我的数据并得到一个占用 24 字节的文件? P.S 我知道将文件另存为 .txt 文件大小为 33 字节。谢谢!
#include <stdio.h>
#include <stdlib.h>
void CreateFile()
{
//Array containing my data
long data[3];
data[0] = 2147483644;
data[1] = 2147483645;
data[2] = 2147483646;
//Initializing a pointer to FILE
FILE *myFilePointer;
//Creating a new file "Longs.myData"
myFilePointer = fopen("Longs.myData", "wb");
//Error handling
if(myFilePointer == NULL){exit(1);}
//Save array to file
fwrite(data, sizeof(long), sizeof(data), myFilePointer);
//sizeof(long) = 8, sizeof(data) = 24 //Total 192
printf("%d %d \n", (int) sizeof(long), (int) sizeof(data));
fclose(myFilePointer);
}
int main()
{
CreateFile();
return 0;
}
在此声明中:
long data[3];
data
的所有三个元素的内存被创建为一个连续的块。根据以下 fwrite()、
原型的第二个和第三个参数
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
- size − This is the size in bytes of each element to be written.
- nmemb − This is the number of elements, each one with a size of size bytes.
写入的总字节数由参数 2 和 3 相乘得出。
所以在下面的语法中,组合参数只是代表了太多的字节:
fwrite(data, sizeof(long), sizeof(data), myFilePointer);
分解...
要写入的总字节数由
给出
sizeof(long)*sizeof(data)
// 8 * 3 * 8 (sizeof(data) == 3 array elements*sizeof(long))
// 8 * 24 = 192
sizeof(long)
的 1 个因素太多了。如果 sizeof(long)
== 8,则计算结果为:
可以通过多种方式编写参数以获得正确的计数。使用 char
(byte
) 作为基本元素,并给定 sizeof(char) is always 1:
fwrite(data, 1, sizeof(data), myFilePointer);
// 1 3*sizeof(long) ==> 1*3*8 == 24
使用 long
作为基本元素,数组大小 (3) 作为元素的数量
fwrite(data, sizeof(long), sizeof(data)/sizeof(data[0]), myFilePointer);
// 8 3 ==> 8*3 == 24
旁白:虽然不是必需的,但在处理二进制文件时最好使用无符号类型:
"unsigned is used to avoid signed [arithmetic] to get in the way, and because it best represents binary contents (a value between 0 and 255)"
(from here)
我有一个长值数组,我想将它们保存到名为“Longs.myData”的文件中。我希望我的文件大小为 24 个字节,因为数组只有 3 个索引。但是,我得到一个 192 字节的文件。有没有办法保存我的数据并得到一个占用 24 字节的文件? P.S 我知道将文件另存为 .txt 文件大小为 33 字节。谢谢!
#include <stdio.h>
#include <stdlib.h>
void CreateFile()
{
//Array containing my data
long data[3];
data[0] = 2147483644;
data[1] = 2147483645;
data[2] = 2147483646;
//Initializing a pointer to FILE
FILE *myFilePointer;
//Creating a new file "Longs.myData"
myFilePointer = fopen("Longs.myData", "wb");
//Error handling
if(myFilePointer == NULL){exit(1);}
//Save array to file
fwrite(data, sizeof(long), sizeof(data), myFilePointer);
//sizeof(long) = 8, sizeof(data) = 24 //Total 192
printf("%d %d \n", (int) sizeof(long), (int) sizeof(data));
fclose(myFilePointer);
}
int main()
{
CreateFile();
return 0;
}
在此声明中:
long data[3];
data
的所有三个元素的内存被创建为一个连续的块。根据以下 fwrite()、
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
- size − This is the size in bytes of each element to be written.
- nmemb − This is the number of elements, each one with a size of size bytes.
写入的总字节数由参数 2 和 3 相乘得出。
所以在下面的语法中,组合参数只是代表了太多的字节:
fwrite(data, sizeof(long), sizeof(data), myFilePointer);
分解...
要写入的总字节数由
sizeof(long)*sizeof(data)
// 8 * 3 * 8 (sizeof(data) == 3 array elements*sizeof(long))
// 8 * 24 = 192
sizeof(long)
的 1 个因素太多了。如果 sizeof(long)
== 8,则计算结果为:
可以通过多种方式编写参数以获得正确的计数。使用 char
(byte
) 作为基本元素,并给定 sizeof(char) is always 1:
fwrite(data, 1, sizeof(data), myFilePointer);
// 1 3*sizeof(long) ==> 1*3*8 == 24
使用 long
作为基本元素,数组大小 (3) 作为元素的数量
fwrite(data, sizeof(long), sizeof(data)/sizeof(data[0]), myFilePointer);
// 8 3 ==> 8*3 == 24
旁白:虽然不是必需的,但在处理二进制文件时最好使用无符号类型:
"unsigned is used to avoid signed [arithmetic] to get in the way, and because it best represents binary contents (a value between 0 and 255)"
(from here)