读二进制文件用C [英] Reading Binary Files in C

查看:207
本文介绍了读二进制文件用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设一个二进制文件(即需要打开读取)写这个函数原型:

 无效writeBinaryFile(字符*文件名字符*评论,诠释numberOfDoubles,双*双,诠释numberOfInts,为int *整数)

(可以在下面的糊中找到有关该功能的更多详细信息)

...

我写成code只获得.bin文件的第一个字母...

下面是我的功能:

 无效readBinaryFile(字符*文件名)
{
        FILE * FP;
        字符* PTR;
        双* ptr1的;
        INT * PTR2;
        PTR =(字符*)malloc的(的sizeof(字符)* 5);
        ptr1的=(双*)malloc的(的sizeof(双)* 6);
        PTR2 =(INT *)malloc的(的sizeof(int)的* 6);        FP = FOPEN(文件名,RB);
        FREAD(PTR,sizeof的(焦炭),11,FP);
        而(* PTR!='\\ 0')
        {
            的printf(%C,* PTR);
            PTR ++;        }
        FCLOSE(FP);
}

我缺少的是在这里吗?我需要阅读的不仅仅是的第一个的字符.bin文件。

详细

帮助是AP preciated,一如既往。谢谢!

在任何情况下,需要充分code以有组织的方式,这里是贴:

http://pastebin.com/66jZwfUD


解决方案

 无效readBinaryFile(字符*文件名)
{
    FILE * FP;
    字符* PTR;
    双* ptr1的;
    INT * PTR2;    PTR =(字符*)malloc的(的sizeof(字符)* 5);
    ptr1的=(双*)malloc的(的sizeof(双)* 6);
    PTR2 =(INT *)malloc的(的sizeof(int)的* 6);

不要投的malloc 在C.它隐藏错误的返回值。

 计划生育=的fopen(文件名,RB);

您需要检查这里的错误。

  FREAD(PTR,sizeof的(焦炭),11,FP);

您分配5个字节 PTR ,现在你正在阅读11.您也应该检查 FREAD

 而(* PTR!='\\ 0')
    {
        的printf(%C,* PTR);
        PTR ++;    }

有什么可prevent这个循环运行了分配的空间的结尾。

  FCLOSE(FP);
}

现在,你分配的​​泄漏,并且由于指针超出范围,当你返回你读入它的数据丢失。内存

Suppose a binary file (that needs to be opened for reading) is written with this function prototype:

void writeBinaryFile ( char *fileName , char *comment , int numberOfDoubles , double *doubles , int numberOfInts , int *ints )

(More details about this function can be found in the paste below)

...

My written code only acquires the FIRST letter of the .bin file...

Here is my function:

void readBinaryFile(char *fileName)
{
        FILE *fp;
        char *ptr;
        double *ptr1;
        int *ptr2;
        ptr=(char*)malloc(sizeof(char)*5);
        ptr1=(double*)malloc(sizeof(double)*6);
        ptr2=(int*)malloc(sizeof(int)*6);

        fp=fopen ( fileName , "rb" ) ;
        fread(ptr,sizeof(char),11,fp);
        while(*ptr!='\0')
        {
            printf("%c",*ptr);
            ptr++;

        }
        fclose(fp);
}

What am I missing here? I need to read more than just the first character in the .bin file.

Help is appreciated, as always. Thanks!

In the case anyone needs the full code in an organized manner, here is the paste:

http://pastebin.com/66jZwfUD

解决方案

void readBinaryFile(char *fileName)
{
    FILE *fp;
    char *ptr;
    double *ptr1;
    int *ptr2;

    ptr=(char*)malloc(sizeof(char)*5);
    ptr1=(double*)malloc(sizeof(double)*6);
    ptr2=(int*)malloc(sizeof(int)*6);

Don't cast the return value of malloc in C. It hides errors.

    fp=fopen ( fileName , "rb" ) ;

You need to check for errors here.

    fread(ptr,sizeof(char),11,fp);

You allocated 5 bytes for ptr, now you're reading 11. You also should check the return value of fread.

    while(*ptr!='\0')
    {
        printf("%c",*ptr);
        ptr++;

    }

There's nothing to prevent this loop from running off the end of the allocated space.

    fclose(fp);
}

Now, all the memory you allocated is leaked and all the data you read into it is lost since the pointers go out of scope when you return.

这篇关于读二进制文件用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆