即使使用正确的语法,fseek()也不会寻找正确的记录

时间:2016-04-07 02:10:14

标签: c fseek

我是c的初学者,我正在尝试根据用户输入的内容搜索二进制文件中的记录,并且使用fseek它根本不返回任何记录。

typedef struct books {
    int ISBN_num;
    char title[60];
    char authorfname[60],authorlname[60];
    char accession_num[7];
    char available[4];
    da borrowdate;
    da returndate;
    char borrowtype[9];
} bks;    

if((bk_temp=fopen("book.dat","rb"))!=NULL){
        puts("Enter ISBN of book you're searching for");
        scanf("%d",&isbn);
            system("cls");
            fseek(bk_temp,sizeof(temp)*(isbn-1),SEEK_SET);
            fread(&temp, sizeof(temp), 1, bk_temp);
            if (temp.ISBN_num == isbn) {
                printf("Title: %s", temp.title);
                printf("Author: %s %s", temp.authorfname, temp.authorlname);
                printf("Accession Number: %s", temp.accession_num);
                printf("Availability: %s", temp.available);
                printf("ISBN Number: %i", temp.ISBN_num);
                getch();
                return temp;
            }else{
                printf("Record not found");
            }
    fclose(bk_temp);

0 个答案:

没有答案