I would like to show data by searching method.
It is working for only first record which meets the entered criteria.
For the next records it's not displaying.
Please help me out.
Thanks in advance
my code
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
void addrecord();
void viewrecord();
void editrecord();
void deleterecord();
void search();
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 main()
{
int ch;
clrscr();
printf("\n\n\t***********************************\n");
printf("\t*SCHOOL MANAGMENT SYSTEM*\n");
printf("\t***********************************");
while(1)
{
printf("\n\n\t\tMAIN MENU:");
printf("\n\n\tADD RECORD\t[1]");
printf("\n\tVIEW RECORD\t[2]");
printf("\n\tEDIT RECORD\t[3]");
printf("\n\tSEARCH\t\t[4]");
printf("\n\tEXIT\t\t[5]");
printf("\n\n\tENTER YOUR CHOICE:");
scanf("%d",&ch);
switch(ch)
{
case 1:
addrecord();
break;
case 2:
viewrecord();
break;
case 3:
editrecord();
break;
case 4:
search();
break;
case 5:
printf("press any key to go back ");
getch();
exit(0);
default:
printf("\nYOU ENTERED WRONG CHOICE..");
printf("\nPRESS ANY KEY TO TRY AGAIN");
getch();
break;
}
system("cls");
}
}
void addrecord( )
{ FILE *fp ;
char another = 'Y' ,time[10];
struct record e ;
char filename[15];
int choice;
system("cls");
printf("\n\n\t\t***************************\n");
printf("\t\t* WELCOME TO THE ADD MENU *");
printf("\n\t\t***************************\n\n");
fflush(stdin);
fp = fopen ("s.txt", "ab+" ) ;
if(fp==NULL)
{
printf("\nSYSTEM ERROR...");
printf("\nPRESS ANY KEY TO EXIT");
getch();
return ;
}
while ( another == 'Y'|| another=='y' )
{
choice=0;
fflush(stdin);
printf ( "\n\tENTER TIME:[hh:mm]:");
scanf("%s",time);
rewind(fp);
while(fread(&e,sizeof(e),1,fp)==1)
{
if(strcmp(e.time,time)==0)
{
printf("\n\tTHE RECORD ALREADY EXISTS.\n");
choice=1;
}
}
if(choice==0)
{
strcpy(e.time,time);
printf("\tENTER NAME:");
fflush(stdin);
gets(e.stud_nm);
fflush(stdin);
printf("\tENTER ENROLLMNT NO:");
scanf("%s",& e.enroll_no);
fflush(stdin);
printf("\tENTER ADDRESS:");
gets(e.addrs);
fflush(stdin);
printf("\tYEAR:");
scanf("%s",e.year);
printf("\tSEM:");
scanf("%s",e.sem);
printf("\tDATE OF BIRTH:");
scanf("%s",e.dob);
fwrite ( &e, sizeof ( e ), 1, fp ) ;
printf("\nYOUR RECORD IS ADDED...\n");
}
printf ( "\n\tADD ANOTHER RECORD...(Y/N) " ) ;
fflush ( stdin ) ;
another = getchar( ) ;
}
fclose ( fp ) ;
printf("\n\n\tPRESS ANY KEY TO EXIT...");
getch();
}
void viewrecord( )
{
FILE *fpte ;
struct record stud;
char time[6],choice,filename[14];
int ch;
system("cls");
printf("\n\n\t\t*******************************\n");
printf("\t\t* HERE IS THE VIEWING MENU *");
printf("\n\t\t*******************************\n\n");
do
{
fflush(stdin);
fpte = fopen ("s.txt", "rb" ) ;
if ( fpte == NULL )
{
puts ( "\nTHE RECORD DOES NOT EXIST...\n" ) ;
printf("PRESS ANY KEY TO EXIT...");
getch();
return ;
}
system("cls");
printf("\n\tHOW WOULD YOU LIKE TO VIEW:\n");
printf("\n\t1.WHOLE RECORD OF THE DAY.");
printf("\n\t2.RECORD OF FIX TIME.");
printf("\n\t\tENTER YOUR CHOICE:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nTHE WHOLE RECORD FOR %s IS:","s.txt");
while ( fread ( &stud, sizeof ( stud ), 1, fpte ) == 1 )
{
printf("\n");
printf("\nTIME: %s",stud.time);
printf("\nENROLLMENT NO: %d",stud.enroll_no);
printf("\nSTUDENT NAME: %s",stud.stud_nm);
printf("\nADDRESS: %s",stud.addrs);
printf("\nYEAR: %d",stud.year);
printf("\nSEM: %d",stud.sem);
printf("\nDATE OF BIRTH: %d",stud.dob);
printf("\n");
}
break;
case 2:
fflush(stdin);
printf("\nENTER TIME:[hh:mm]:");
gets(time);
while ( fread ( &stud, sizeof ( stud ), 1, fpte ) == 1 )
{
if(strcmp(stud.time,time)==0)
{
printf("\nYOUR RECORD IS:");
printf("\nENROLLMENT NO: %d",stud.enroll_no);
printf("\nTIME: %s",stud.time);
printf("\nSTUDENT NAME: %s",stud.stud_nm);
printf("\nADDRESS: %s",stud.addrs);
printf("\nYEAR: %d",stud.year);
printf("\nSEM: %d",stud.sem);
printf("\nDATE OF BIRTH: %d",stud.dob);
}
}
break;
default: printf("\nYOU TYPED SOMETHING ELSE...\n");
break;
}
printf("\n\nWOULD YOU LIKE TO CONTINUE VIEWING...(Y/N):");
fflush(stdin);
scanf("%c",&choice);
}while(choice=='Y'||choice=='y');
fclose ( fpte ) ;
return ;
printf("\n\n\tPRESS ANY KEY TO EXIT...");
getch();
}
void editrecord()
{
FILE *fpte ;
struct record r;
char time[6],choice,filename[14];
int num,count=0;
system("cls");
printf("\n\n\t\t*******************************\n");
printf("\t\t* WELCOME TO THE EDITING MENU *");
printf("\n\t\t*******************************\n\n");
do
{
fflush(stdin);
printf("\n\tENTER TIME:[hh:mm]:");
gets(time);
fpte = fopen ("s.txt", "rb+" ) ;
if ( fpte == NULL )
{
printf( "\nRECORD DOES NOT EXISTS:" ) ;
printf("\nPRESS ANY KEY TO GO BACK");
getch();
return;
}
while ( fread ( &r, sizeof ( r ), 1, fpte ) == 1 )
{
if(strcmp(r.time,time)==0)
{
printf("\nYOUR OLD RECORD WAS AS:");
printf("\nTIME: %s",r.time);
printf("\nENROLLMENT NO: %d",r.enroll_no);
printf("\nSTUDENT NAME: %s",r.stud_nm);
printf("\nADDRESS: %s",r.addrs);
printf("\nYEAR: %d",r.year);
printf("\nSEM: %d",r.sem);
printf("\nDATE OF BIRTH: %d",r.dob);
printf("\n\n\t\tWHAT WOULD YOU LIKE TO EDIT..");
printf("\n1.TIME.");
printf("\n2.STUDENT NAME.");
printf("\n3.ADDRESS");
printf("\n4.DATE OF BIRTH.");
printf("\n5.YEAR.");
printf("\n6.WHOLE RECORD.");
printf("\n7.GO BACK TO MAIN MENU.");
do
{
printf("\n\tENTER YOUR CHOICE:");
fflush(stdin);
scanf("%d",&num);
fflush(stdin);
switch(num)
{
case 1: printf("\nENTER THE NEW DATA:");
printf("\nNEW TIME:[hh:mm]:");
gets(r.time);
break;
case 2: printf("\nENTER THE NEW DATA:");
printf("\n NEW STUDENT NAME:");
gets(r.stud_nm);
break;
case 3: printf("\nENTER THE NEW DATA:");
printf("\nNEW ADDRESS:");
gets(r.addrs);
break;
case 4: printf("\nENTER THE NEW DATA:");
printf("\nDATE OF BIRTH:");
scanf("%d",&r.dob);
break;
case 5: printf("ENTER THE NEW DATA:");
printf("\nYEAR:");
scanf("%d",&r.year);
break;
case 6: printf("\nENTER THE NEW DATA:");
strcpy(r.time,time);
printf("\tENTER NAME:");
fflush(stdin);
gets(r.stud_nm);
fflush(stdin);
printf("\tENTER ENROLLMNT NO:");
scanf("%d",& r.enroll_no);
fflush(stdin);
printf("\tENTER ADDRESS:");
gets(r.addrs);
fflush(stdin);
printf("\tYEAR:");
scanf("%d",r.year);
printf("\tSEM:");
scanf("%d",r.sem);
printf("\tDATE OF BIRTH:");
scanf("%d",r.dob);
break;
case 7: printf("\nPRESS ANY KEY TO GO BACK...\n");
getch();
return ;
default: printf("\nYOU TYPED SOMETHING ELSE...TRY AGAIN\n");
break;
}
}while(num<1||num>8);
fseek(fpte,-sizeof(r),SEEK_CUR);
fwrite(&r,sizeof(r),1,fpte);
fseek(fpte,-sizeof(r),SEEK_CUR);
fread(&r,sizeof(r),1,fpte);
choice=5;
break;
}
}
if(choice==5)
{
system("cls");
printf("\n\t\tEDITING COMPLETED...\n");
printf("--------------------\n");
printf("THE NEW RECORD IS:\n");
printf("--------------------\n");
printf("\nTIME: %s",r.time);
printf("\nENROLLMENT NO: %d",r.enroll_no);
printf("\nSTUDENT NAME: %s",r.stud_nm);
printf("\nADDRESS: %s",r.addrs);
printf("\nYEAR: %d",r.year);
printf("\nSEM: %d",r.sem);
printf("\nDATE OF BIRTH: %d",r.dob);
fclose(fpte);
printf("\n\n\tWOULD YOU LIKE TO EDIT ANOTHER RECORD.(Y/N)");
scanf("%c",&choice);
count++;
}
else
{
printf("\nTHE RECORD DOES NOT EXIST::\n");
printf("\nWOULD YOU LIKE TO TRY AGAIN...(Y/N)");
scanf("%c",&choice);
}
}while(choice=='Y'||choice=='y');
fclose ( fpte ) ;
if(count==1)
printf("\n%d FILE IS EDITED...\n",count);
else if(count>1)
printf("\n%d FILES ARE EDITED..\n",count);
else
printf("\nNO FILES EDITED...\n");
printf("\tPRESS ENTER TO EXIT EDITING MENU.");
getch();
}
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(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;
}
}