C语言 二进制文件的EOF是什么



我的程序需要从二进制文件读取到链表,这个函数做得很好并为它分配了正确的内存,但由于某种原因,它在中断之前做了另一个循环。 试图为它寻找一个好的解决方案,但没有运气,链表中的最后一个结构被垃圾了。

结构:

typedef struct
{
    char id[10];
    char * first_name;
    char * last_name;
    int age;
    char gender;
    char * username;
    char * password;
    char * description;
    int hobbies[4];
    struct Person * next_client;
}Person;

下面是一些代码:

Person * input_from_file(Person * member)
{
int str_size;
Person * clients_start = NULL;
FILE * filePointerRead;
filePointerRead = fopen("input.bin", "rb");
if (filePointerRead != NULL){
    while (1){
        member = NULL;
        member = (Person*)malloc(sizeof(Person));
        fread(&member->id, sizeof(char), ID_DIGITS + 1, filePointerRead);
        fread(&str_size, sizeof(int), 1, filePointerRead);
        member->first_name = (char*)malloc(str_size*sizeof(char));
        fread(member->first_name, sizeof(char), str_size, filePointerRead);
        //more reading from file
        member->next_client = NULL;
        clients_start = receive_clients_info(clients_start, member);  //function to put the received struct from file to end of the linked list
        if (feof(filePointerRead))
            break;
    }
    fclose(filePointerRead);
}
return clients_start;
}

调用feof的问题在于,除非您在EOF时尝试读取,否则它不会返回"true"。换句话说,如果您的文件正好有 100 个字节,并且您已成功尝试读取正好 100 个字节,则feof将返回"false",直到您尝试再读取至少一个字节。

这就是为什么你应该避免feof而检查 fread 的返回值的原因,它告诉你从文件中读取了多少字节:

if (fread(&member->id, sizeof(char), ID_DIGITS + 1, filePointerRead) != ID_DIGITS + 1) {
    // The code above knows that sizeof(char) is always 1.
    // For other data types you need to compute the actual size
    // by multiplying sizeof(T) by the number of items that you read.
    break;
}

在你打电话给fread的所有地方都做同样的事情。

!=进行比较有效,因为它始终返回fread可以完成请求时传递的确切大小:

成功完成后,仅当遇到读取错误或文件末尾时,fread()才应返回成功读取的元素数小于nitems

检查fread()而不是foef(),堆栈溢出上有很多关于这个问题的答案,feof()设置 EOF 指标时返回 true,fread()在读取文件末尾时会设置它。

当到达文件末尾时,fread()将返回0或小于请求的字节,但您的程序需要一个额外的循环,在该循环中,fread()尝试读取超过文件末尾的内容,并且它将设置EOF指示器。

看到这个"while( !feof( file ) )"总是错误的

您遇到的问题是 feof() 在设置文件的 EOF 标志之前不会返回 true,并且在尝试读取文件失败之前不会设置文件的 EOF 标志,因为文件中没有剩余的数据可供读取。

下面是一个示例:假设文件中有 1 个字节,并且您有一个循环,一次读取一个字节的文件。

第一次通过循环时,读取一个字节并返回给程序。如果程序测试 feof(),它仍将返回 FALSE,因为读取文件成功。

第二次通过循环时,文件中的所有字节都已被读取,因此读取 0 byes 并返回到程序,此时设置 EOF 标志是因为读取文件由于到达末尾而失败。此时 feof() 将返回 TRUE。

在我的示例中,即使文件中只有一个字节,您也遍历了两次循环。同样的情况也发生在您的代码上。

要解决此问题,请始终检查 fread() 调用的结果。它返回读取的项目数(而不是字节数)。顺便说一下,fread() 将始终读取整个项目,而不是部分项目。如果 fread() 返回的项目比预期的少,请脱离循环。 通常,你会脱离循环,因为你已经到达了文件的末尾,但有可能还有其他错误 - 也许有人将电源线拉到外部硬盘驱动器。如果你想看看为什么fread()没有读取任何内容,你可以使用feof()或ferror()。

any easy way to accomplish the task is:
calculating the total length of each complete record in the file
 (from your code, I assume all records are the same length) 
fopen( ..inputfile.. )
if fopen not successful, 
then
    perror()
    exit( EXIT_FAILURE );
endif
// implied else, fopen successful
while(completelength == fread( the complete length into a local buffer))
{ 
    ...extract each field from local buffer into struct...
    ...place struct into linked list... 
}  
//when get here, reading/building linked list is done 
fclose( inputfile ) 
... 

最新更新