为什么从随机访问文件中读取数据并在屏幕上打印两次 table 最后一行? (有图)

Why data read from the random access file and print twice of last line of the table on screen ? (Got picture)

请问为什么下面table的最后一行会打印两次?一切都很好,只是出现了上述问题。

使用的文件类型是随机访问文件,数据写入名为 toyota.txt 的二进制文件。

任何人都可以教我那里有什么问题?非常感谢!

代码如下:

/*program 1 write the data into file toyota.txt*/

#include<stdio.h>

struct vehicledata{
    char vnum[100];
    int cnum;
    float f50;
    float f100;
    float f200;
    float f400;
    float f800;
};

int main()
{
    FILE *fptr;
    struct vehicledata toyota;
    if((fptr = fopen("toyota.txt","wb")) == NULL)
    {
        printf("Error! File fail opened!");
    }
    else
    {
        printf("This program can create a text file named toyota.txt \n");
        printf("and save the value of car noise level(dB) with\ndifferent frequency and vehicle numbers into file\n\n");
        printf("======================================================================\n");
        printf("Enter car number(1 to 10, enter 0 to end input)\n");
        printf("? ");
        scanf("%d", &toyota.cnum);

        while (toyota.cnum != 0)
        {
            printf("\nEnter Vehicle number\n");
            printf("? ");
            scanf("%s", toyota.vnum);
            printf("Enter car noise level(dB) at 50Hz, 100Hz, 200Hz, 400Hz and 800Hz\n");
            printf("? ");
            scanf("%f %f %f %f %f", &toyota.f50, &toyota.f100, &toyota.f200, &toyota.f400, &toyota.f800);
            fseek(fptr, (toyota.cnum - 1) * sizeof(struct vehicledata), SEEK_SET);
            fwrite(&toyota, sizeof(struct vehicledata), 1, fptr);
            printf("\n======================================================================\n");
            printf("\nEnter car number (1 to 100, 0 to end input) \n");
            printf("? ");
            scanf("%d", &toyota.cnum);
        }
    }
    fclose(fptr);
    return 0;
}
/*program 2 reads the data from the file toyota.txt and find
the car that emanates the minimum noise at 200Hz*/
#include <stdio.h>
#include <stdlib.h>

struct vehicledata{
    char vnum[100];
    int cnum;
    float f50;
    float f100;
    float f200;
    float f400;
    float f800;
};
char name[10];
float min;
int i, m;
int main()
{
    FILE *fptr;
    struct vehicledata toyota;
    if ((fptr = fopen("toyota.txt","rb")) == NULL)//open and read data in the file
    {
        printf("Error! File fail opened!");
    }
    else
    {
        printf("This program can read and display the data from the toyota.txt\n and find car that have minimum noise in 200Hz\n\n");
        printf("\nThe information in the toyota.txt are shown at below:\n");
        printf("\n\t\t\t\tCar Noise Level(dB)\t\t\t");
        printf("\n=======================================================================\n");
        printf("%s\t\t%s\t%s\t%s\t%s\t%s\t\n","Vehicle Number","50Hz","100Hz","200Hz","400Hz","800Hz");
        printf("=======================================================================\n");
        char name[10];
        float min=100;
        while(!feof(fptr))
        {
            fread(&toyota, sizeof(struct vehicledata), 1, fptr);
            if(toyota.f200 < min)
           {
               min=toyota.f200;
               for(int i=0 ; i<10 ; i++)
                {
                    name[i]=toyota.vnum[i];
                }
           }
            if(toyota.cnum != 0)
            {
                printf("%s\t\t\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\n", toyota.vnum, toyota.f50, toyota.f100, toyota.f200, toyota.f400, toyota.f800);
            }
        }
        printf("=======================================================================\n");
        printf("\n\n+++++++++++++++++++++++++++++++++Result+++++++++++++++++++++++++++++++++\n");
        printf("\nVehicle number of car with minimum noise in 200Hz is %s at %.2f dB\n", name, min);
    }
    fclose(fptr);
    return 0;
}

问题出在程序 2 中。在下面的部分中,您假设您的 fread 没问题,因为您没有遇到 EOF。事实并非如此。

while(!feof(fptr))
{
    fread(&toyota, sizeof(struct vehicledata), 1, fptr);

发生的事情是您在循环中检查 EOF 并且在文件的最后 fread 确实被读取了一次但失败了。由于您没有检查它并且 fread 没有 toyota 被覆盖(因为读取失败),所以您有一个副本可以打印出来。之后,您现在检查 EOF 并离开循环。

更好的循环应该是这样的:

while (fread(&toyota, sizeof(struct vehicledata), 1, fptr)) {

由于 fread 的文档指出,“如果发生错误,或到达文件末尾,return 值是一个短项目计数(或零)。”,在你的情况下,我们并没有真正检查错误,我们在 EOF。

但是如果您想知道循环是由于 EOF 还是错误而结束的,那么“调用者必须使用 feof(3) 和 ferror(3) 来确定发生了什么。".