我有一个大问题需要解决,然后才能继续我的程序。
我必须打开一个二进制文件,读取它的内容,将内容保存到缓冲区中,用 malloc 在堆上分配空间,关闭文件,最后 printf(.bin 文件的内容)。我走了这么远(关闭文件尚未实现):
void executeFile(char *path){
FILE *fp; /*filepointer*/
size_t size; /*filesize*/
unsigned int buffer []; /*buffer*/
fp = fopen(path,"rb"); /*open file*/
fseek(fp, 0, SEEK_END);
size = ftell(fp); /*calc the size needed*/
fseek(fp, 0, SEEK_SET);
buffer = malloc(size); /*allocalte space on heap*/
if (fp == NULL){ /*ERROR detection if file == empty*/
printf("Error: There was an Error reading the file %s \n", path);
exit(1);
}
else if (fread(&buffer, sizeof(unsigned int), size, fp) != size){ /* if count of read bytes != calculated size of .bin file -> ERROR*/
printf("Error: There was an Error reading the file %s - %d\n", path, r);
exit(1);
}else{int i;
for(i=0; i<size;i++){
printf("%x", buffer[i]);
}
}
}
我想我弄乱了缓冲区,我不确定我是否正确读取了 .bin 文件,因为我无法使用它进行打印printf("%x", buffer[i])
希望大家能帮忙
来自德国的问候 :)