搜索文件中的字符串并将其与使用C输入的变量进行比较

搜索文件中的字符串并将其与使用C输入的变量进行比较,c,arrays,file,string.h,conio,C,Arrays,File,String.h,Conio,我想在程序中输入一个名称,该名称将作为文件的关键字。程序将在文件中搜索该名称,并显示与之相关的所有其他信息。下面是一段代码 #include<conio.h> #include<stdio.h> #include<windows.h> #include<string.h> FILE *ptr; int bookgenre; char fname[20], lname[20],book[20],gender[20]; struct persfil

我想在程序中输入一个名称,该名称将作为文件的关键字。程序将在文件中搜索该名称,并显示与之相关的所有其他信息。下面是一段代码

#include<conio.h>
#include<stdio.h>
#include<windows.h>
#include<string.h>


FILE *ptr;
int bookgenre;
char fname[20], lname[20],book[20],gender[20];
struct persfile{
    char fname[20];
    char lname[20];
    char bookname[20];
    int  date, latefees;
    char gender;
};
void menu ();
void caseCheck(int);

int main(void)
{
    int New = 0, genrenum = 0, count=0;
    struct persfile personfile[count]; //Intialization of the struct in the main function
    menu();

    /*New = getch();*/
    scanf("%d",&New);
        system ("cls");

        if(New == 2)
        {
         for (count=1; count<=200; count++ )
         {

            int latefees=0;
            ptr = fopen("membersit.txt", "a");

             /*for (count=1;count<=5;count++)*/


            printf("Enter the first name of the person \n");
            scanf("%s", personfile[count].fname);
            printf("Enter the last name of the person \n\n");
            scanf("%s", personfile[count].lname);
            printf("Enter the gender of the individual....(M/F) \n");
            personfile[count].gender = getch();
            printf("%c", personfile[count].gender);
                                                            // At a later date...examine the time library and look at its connection to the
            system("cls");                                          //date at which the book was rented.. //

            printf("The code for the differnt book genres are given below\n\n");
            printf("000-Kids...111-Supernatural...222-Gaming\n\n");
            printf("_____________________\n\n");


            printf("Enter the genre number of said book\n");
            scanf("%d", &genrenum);

            system ("cls");
            printf("These are the books under this genre\n\n");
            printf("______________");
            system("cls");

            caseCheck(genrenum);
            scanf("%s",book);

            printf("Since this is a new individual..he has no outstandng latefees");
            fprintf(ptr,"%s %s %d \n", personfile[count].fname, personfile[count].lname, personfile[count].latefees);
            fclose((ptr));
            system("cls");
         }
        } 

        else if(New==3)
        {

        ptr=fopen("membersit.txt", "r");

        fscanf(ptr,"%s %s",fname,  lname);


        printf("First Name: %s \n Last Name: %s \n  Latefees: %d  \n", fname, lname);

        fclose((ptr));


    /*if (ptr==NULL){
        printf("This file is not in our system");
        exit(0);
     }*/
        }

    /*search the existing files for the name of the person and open up their file
    The opened file would have late fees as a header..*/



system("Pause");
return 0;
};


void menu ()

{
    printf("Welcome to the library!!");
    printf("\n___________________________________________________");
    printf("\n\nWhat would you like to do?\n");
    printf("\n1.Add information to existing member \n\n2.Create a new member");
    printf("\n\n3.Search a member\n\n4.Display all members\n\5.End the program\n\n\n\n");
}

void caseCheck(int genrenum)
{
    switch (genrenum)
    {
        case 000:
            printf("KIDS BOOKS\n_________________________________________\nDora the explorer\nBlues Clues\nGo Diego Go\nThe Little Engine Who Could\n Curious George\n\n");
        break;

        case 111:
            printf("SUPERNATURAL BOOKS\n__________________________________\nLost\nHarry Potter\nAnnie/\n\n");
            break;

        default:
        printf("The data you entered is invalid");
    }
}

/* Notes :
 Good programming practices
 Your '}' should line up when using condition loops
 -> Variables shoul not be started wth a capital letter */

 /*There was something wrong with your while loop.... you did not implement it correctly... Leave off the terminate function for last

代码需要在缩进方面保持一致。某些编译器将生成代码,当最后一个案例缺少“break;”时,这些代码将崩溃语句函数的右大括号“}”没有尾随“;”因此,这段代码的编译并不干净。您应该在运行编译器时启用所有警告,以便它可以告诉您诸如尾随“;”之类的错误字符发布的代码似乎包含了一些选项卡,这会弄乱代码的格式。不同的人、不同的编译器等可以使用不同的宽度进行缩进和制表符宽度。强烈建议不要在源代码中使用制表符,并使用可读的缩进宽度。使用非单宽字体时,不会显示缩进宽度2。所以我建议使用4个空格。你对这段代码有什么特别的问题吗?建议在文件范围内声明的数据,其他文件不会访问这些数据,总是使用C中的“static”修饰符