小编use*_*126的帖子

读取二进制文件,保存在缓冲区中,打印出缓冲区的内容

在我继续我的程序之前,我有一个需要解决的大问题.

我必须打开一个二进制文件,读取它的内容,将内容保存到缓冲区,使用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)

c malloc buffer binaryfiles fread

4
推荐指数
1
解决办法
3万
查看次数

标签 统计

binaryfiles ×1

buffer ×1

c ×1

fread ×1

malloc ×1