我正在尝试动态填充数组。软件以二进制模式打开文件,获取文件的维度并初始化数组以存储打开的文件的二进制内容。然后它创建一个新文件,在其中放入数组的内容。
这样可行,但仅适用于小于1 MB的文件。
数组是否具有最大维度?
以下是代码:
void read(char* address){
int i;
int d;
int size;
unsigned char standard = 0x00;
// get file dimension:
FILE* sizer = fopen(address, "r+b");
fseek(sizer, 0, SEEK_END);
size = ftell(sizer);
fseek(sizer, 0, SEEK_SET);
fclose(sizer);
// initialization of array:
unsigned char loaded[size];
// filling the array with the binary contents of file:
FILE* load = fopen(address, "r+b");
for(i=0;i<size;i++)
{
fseek(load, i, SEEK_SET);
fread(&loaded[i], sizeof(standard), 1, load);
}
fclose(load);
// Copy the binary contents of array, inside a new file:
FILE* write = fopen("output.extension", "w+b");
for(i=0;i<size;i++)
{
fseek(write, i, SEEK_SET);
fwrite(&loaded[i], sizeof(standard), 1, write);
}
fclose(write);
}