C语言 读取二进制文件,存入缓冲区,打印出缓冲区内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16738986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 06:27:42  来源:igfitidea点击:

Read binary file, save in buffer, print out content of buffer

cmallocbufferbinaryfilesfread

提问by user2418126

I have a big problem that need's to be solved before I can continue with my program.

我有一个大问题需要解决,然后才能继续我的程序。

I have to open a binary file, read it's content, save the content into a buffer, allocate space on the heap with malloc, close the file and finally printf( the content of the .bin file). I came this far (closing file is not implemented yet):

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

I think I messed up the buffer and I am not really sure if I read the .bin file correctly because I can't print it with printf("%x", buffer[i])

我想我弄乱了缓冲区,我不确定我是否正确读取了 .bin 文件,因为我无法打印它 printf("%x", buffer[i])

Hope you guys can help

希望大家帮帮忙

Greetings from germany :)

来自德国的问候 :)

回答by chux - Reinstate Monica

Recommended changes:

建议更改:

1) Change your buffer to a char(byte), as the ftell()will report the size in bytes (char) and malloc()use the byte size also.

1) 将缓冲区更改为char(byte),因为它ftell()会以字节 ( char)为单位报告大小并也malloc()使用字节大小。

unsigned int buffer []; /*buffer*/

to

unsigned char *buffer; /*buffer*/

2) This is OK, size is bytes and buffer points to bytes, but could be explicitly cast

2) 这没问题,大小是字节,缓冲区指向字节,但可以显式转换

buffer = malloc(size);  /*allocate space on heap*/

to

buffer = (unsigned char *) malloc(size);  /*allocate space on heap*/
/* or for those who recommend no casting on malloc() */
buffer = malloc(size);  /*allocate space on heap*/

3) change 2nd parameter from sizeof(unsigned int)to sizeof *buffer, which is 1.

3) 将第二个参数从sizeof(unsigned int)改为sizeof *buffer,即 1。

else if (fread(buffer, sizeof(unsigned int), size, fp) != size){

to

else if (fread(buffer, sizeof *buffer, size, fp) != size){ 

4) Change "%x"to "%02x"else single digit hexadecimal numbers will confuse the output. E. g. is "1234" four bytes or two?

4) 更改"%x""%02x"else 一位十六进制数字会混淆输出。例如 “1234”是四个字节还是两个字节?

printf("%x", buffer[i]);

to

printf("%02x", buffer[i]);

5) Your clean-up at the end of the function may include

5) 您在函数结束时的清理可能包括

fclose(fp);
free(buffer);