在我继续我的程序之前,我有一个需要解决的大问题.
我必须打开一个二进制文件,读取它的内容,将内容保存到缓冲区,使用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 != …Run Code Online (Sandbox Code Playgroud)