简体   繁体   English

读取c中的二进制文件,直到文件结尾

[英]reading binary file in c until the end of file

I have a binary file with some lines into it(I don't know how much exactly) and I want to read this lines into struct until the end of file and then rewrite this lines in new file.txt. 我有一个带有一些行的二进制文件(我不知道确切多少),我想将此行读入struct直到文件末尾,然后将这些行重写到新的file.txt中。 My question is: How can I read from binary file into struct until the end of file? 我的问题是:如何从二进制文件读入struct直到文件结束? It prints only first 11 lines. 它仅打印前11行。 Must I allocate memory for this or smth? 我必须为此分配内存吗?

    struct linien
    {
        short x1, x2, y1, y2;
        unsigned char R, G, B;
    };


    FILE *fp; // pointer to a file type
    FILE *fpa; // pointer to a file type

    int counter;
    struct linien x; //x is a variable of type struct linien

    //open files - one for reading and another one for writing
    fp = fopen("\\Linien.pra", "rb");
    fpa = fopen("\\Linien.txt", "w");

    //check to see if files opened succesfully 
    if ((fp == NULL)||(fpa == NULL))
    {
        printf("file failed to open\n");
    }

the for loop doesn't seem to work correctly. for循环似乎无法正常工作。

    else
    {
        for (counter = 0; counter < sizeof(x); counter++) //print and write lines
        {
            //read the file Linien.pra
            fread(&x, sizeof(x), 1, fp);
            printf("%2d\t %3d\t %4d\t %5d\t %6d\t %7d  %8d\n", x.x1, x.x2, x.y1, x.y2, x.R, x.G, x.B);  
            //write struct linien to new file Linien.txt
            fprintf(fpa, "%2d\t %3d\t %4d\t %5d\t %6d\t %7d  %8d\n", x.x1, x.x2, x.y1, x.y2, x.R, x.G, x.B);
        }

        fclose(fp); // close file
        fclose(fpa); // close file
    }

You should check the return value of fread so you can use fread in a while loop like that: 您应该检查fread的返回值,以便可以在while循环中使用fread,如下所示:

while (fread(&x, sizeof(x), 1, fp) != 0)
{
}

Make sure the structs in file match with the struct linien. 确保文件中的结构与linien结构匹配。 Check for the return value of fread , to check if you reached the end of the file. 检查fread的返回值,以检查是否到达文件末尾。

It prints only first 11 lines 它仅打印前11行

Let's see .... 让我们来看看 ....

struct linien
{
    short x1, x2, y1, y2;
    unsigned char R, G, B;
};

int counter;
struct linien x;

// [...]

for (counter = 0; counter < sizeof(x); counter++)
{
    // code to read **one** instance of `struct linien`
}

See something? 有事吗 What's sizeof(x) ? sizeof(x)多少? We have 4 short and 3 char in struct linien -- assuming your typical platform where short has a size (and alignment requirement) of 2, this makes a total of 2*4 + 3 = 11. Suprise? 我们有4 short 3 charstruct linien -假设典型的平台,在这里short的大小为2(和对齐要求),这导致目前共有2 * 4 + 3 = 11的惊喜呢? ;) ;)

You're looping exactly 11 times, for what reason ever. 出于什么原因,您恰好循环11次。 So you read (and write) 11 items! 因此,您读(写)了11个项目!

Instead, you should only stop once your fread() call fails (test the return value!). 相反,您仅应在fread()调用失败后停止(测试返回值!)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM