hey all!!
i m making a program in which i want to search record by name, year and, enrollment no.

my code is working only for the first record of a file , its not usfull for second record or any record after the first record

i've tried toooooo much bt cat find solution..
somebody plz help me out
my code is

struct record

{

    char time[6];

    char stud_nm[30];

    char addrs[25];

    char cnt_no;

    char enroll_no[20];

    char sem;

    char dob;

    char year[10];


} ;



  void search()
    {

        FILE *fp;
        struct record sch;

        char nm[100];
        char enroll[20];
        char yr[20];
        int ch,i;



        system("cls");
        printf("\n\tSEARCH STUDENT'S RECORD\n\t");
        printf("\n\t [1].SEARCH BY NAME\n\t");
        printf("\n\t [2].SEARCH BY ENROLLMENT NO\n\t");
        printf("\n\t [3].SEARCH BY YEAR\n\t");

        printf("\nEnter your choice :");
        scanf("%d",&ch);

        switch(ch)
        {
            case 1:

                printf("\nEnter name : ");
                scanf("%s",nm);
                   printf("%s",nm);
                fp=fopen("s.txt","rb");

                  //    while(!feof(fp))
                  while(fread(&sch,sizeof(sch),1,fp)==1)
                {

                  fread(&sch,sizeof(sch),1,fp);

                    if(strcmp(nm,sch.stud_nm)==0)
                     {

                        printf("\nYOUR RECORD IS:");

                        printf("\nENROLLMENT NO: %s",sch.enroll_no);

                        printf("\nSTUDENT NAME: %s",sch.stud_nm);

                        printf("\nADDRESS: %s",sch.addrs);

                        printf("\nYEAR: %s",sch.year);

                        printf("\nDATE OF BIRTH: %s",sch.dob);



                         getch();

                    }



                }
                  fclose(fp);
                    break;

            case 2:
                  printf("\nEnter Enrollment no :");
                  scanf("%s",enroll);

                fp=fopen("s.txt","rb");

                while(!feof(fp))
                {

                  fread(&sch,sizeof(sch),1,fp);

                    if(strcmp(enroll,sch.enroll_no)==0)
                     {

                        printf("\nYOUR RECORD IS:");

                        printf("\nENROLLMENT NO: %s",sch.enroll_no);

                        printf("\nSTUDENT NAME: %s",sch.stud_nm);

                        printf("\nADDRESS: %s",sch.addrs);

                        printf("\nYEAR: %s",sch.year);

                        printf("\nDATE OF BIRTH: %s",sch.dob);

                         getch();

                    }

                }

                break;
             case 3:
                printf("Enter year:");
                scanf("%s",yr);

                fp=fopen("s.txt","rb");

                while(!feof(fp))
                {

                  fread(&sch,sizeof(sch),2,fp);

                    if(strcmp(yr,sch.year)==0)
                     {

                        printf("\nYOUR RECORD IS:");

                        printf("\nENROLLMENT NO: %s",sch.enroll_no);

                        printf("\nSTUDENT NAME: %s",sch.stud_nm);

                        printf("\nADDRESS: %s",sch.addrs);

                        printf("\nYEAR: %s",sch.year);

                        printf("\nDATE OF BIRTH: %s",sch.dob);

                         getch();

                    }

                }

                break;
             default:
                  printf("Wrong choice");
                  exit(0);
                  break;
           }
    }

Edited 2 Years Ago by selfridgegirl: wrolgy added

This article has been dead for over six months. Start a new discussion instead.