Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<conio.h>
- #include<string.h>
- #include<stdlib.h>
- #include<math.h>
- #define Student struct Stud
- void add(FILE * fp); //to add to list
- void display(FILE * fp);//display whole list
- void searchRecord(FILE *fp);//find a particular record
- void command(char ch,int n);//printing a character ch n times
- void title();//printing head line for each screen
- struct Stud
- {
- int class;
- char name[100];
- int id;
- char fname[100];
- char mname[100];
- char mobile[13];
- float sgpa[3];
- float cgpa;
- };
- int main()
- {
- FILE * fp;
- int option;
- char another;
- if((fp=fopen("studentInfo.txt","rb++"))==NULL)
- {
- if((fp=fopen("studentInfo.txt","wb++"))==NULL)
- {
- printf("can't open file");
- return 0;
- }
- }
- title();
- printf("\n\t\tpress any key to continue");
- getch();
- while(1)
- {
- title();
- printf("\n\t");
- command('-',64);
- printf("\n\n\t\t\t1. ADD Student");
- printf("\n\n\t\t\t2. DISPLAY Student LIST");
- printf("\n\n\t\t\t3. Search Record");
- printf("\n\n\t\t\t0. EXIT");
- printf("\n\n\t\tEnter Your Option :--> ");
- scanf("%d",&option);
- switch(option)
- {
- case 0:
- return 1;
- break;
- case 1:
- add(fp);
- break;
- case 2:
- display(fp);
- break;
- case 3:
- searchRecord(fp);
- break;
- default:
- printf("\n\t\tYou Pressed wrong key");
- printf("\n\t\tProgram terminated");
- getch();
- exit(0);
- }
- }
- return 1;
- }
- //----printing character ch at n times ------
- void command(char ch,int n)
- {
- while(n--)
- {
- putchar(ch);
- }
- }
- //-----Printing Head Line of the program -----
- void title()
- {
- system("cls");
- printf("\n\n\t");
- command('*',16);
- printf("[STUDENT] [INFORMATION] [SYSTEM]");
- command('*',16);
- printf("\n");
- }
- // ==========ADDING NEW RECORD==========================
- void add(FILE * fp)
- {
- title();
- char another='y';
- Student s;
- int i;
- float cgpa;
- fseek(fp,0,SEEK_END);
- while(another=='y'||another=='Y')
- {
- printf("\n\n\t\tEnter class number \t");
- scanf("%d",&s.class);
- printf("\n\n\t\tEnter id number \t");
- scanf("%d",&s.id);
- printf("\n\n\t\tEnter Full Name of Student\t");
- fflush(stdin);
- fgets(s.name,100,stdin);
- s.name[strlen(s.name)-1]='\0';
- printf("\n\n\t\tEnter Full Name of Student's Fathers's name\t");
- fflush(stdin);
- fgets(s.fname,100,stdin);
- s.fname[strlen(s.fname)-1]='\0';
- printf("\n\n\t\tEnter Full Name of Student's Mother's name\t");
- fflush(stdin);
- fgets(s.mname,100,stdin);
- s.mname[strlen(s.mname)-1]='\0';
- printf("\n\n\t\tEnter Student's Mobile number\t");
- fflush(stdin);
- fgets(s.mobile,13,stdin); //fgets takes an extra \n character as input
- s.mobile[strlen(s.mobile)-1]='\0';
- printf("\n\n\tEnter SGPA for 3 semesters\n\t");
- for(i=0,cgpa=0; i<3; i++)
- {
- scanf("%f",&s.sgpa[i]);
- cgpa+=s.sgpa[i];
- }
- cgpa/=3.0;
- s.cgpa=cgpa;
- fwrite(&s,sizeof(s),1,fp);
- printf("\n\n\t\tWant to enter another student info (Y/N)\t");
- fflush(stdin);
- another=getchar();
- }
- }
- //====================DISPLAY THE LIST =================
- void display(FILE * fp)
- {
- title();
- Student s;
- int i,siz=sizeof(s);
- rewind(fp);
- while((fread(&s,siz,1,fp))==1)
- {
- printf("\n\n\t\tCLASS : %d",s.class);
- printf("\n\n\t\tID : %d",s.id);
- printf("\n\t\tNAME : %s",s.name);
- printf("\n\n\t\tFather's Name : %s",s.fname);
- printf("\n\n\t\tMOther's Name : %s",s.mname);
- printf("\n\n\t\tMOBILE : %s",s.mobile);
- printf("\n\n\tSGPA: ");
- for(i=0; i<3; i++)
- printf("| %.2f |",s.sgpa[i]);
- printf("\n\n\t\tCGPA : %.2f\n\t",s.cgpa);
- command('-',65);
- }
- printf("\n\n\n\t");
- command('*',65);
- printf("\n\n\t");
- system("pause");
- }
- void searchRecord(FILE *fp)
- {
- title();
- int tempId,flag,siz,i,tempclass;
- Student s;
- char another='y';
- siz=sizeof(s);
- while(another=='y'||another=='Y')
- {
- printf("\n\n\tEnter Class Number of a Student to search the record : ");
- scanf("%d",&tempclass);
- rewind(fp);
- while((fread(&s,siz,1,fp))==1)
- {
- if(s.class==tempclass)
- {
- printf("\n\n\tEnter ID Number of a Student to search the record : ");
- scanf("%d",&tempId);
- rewind(fp);
- while((fread(&s,siz,1,fp))==1)
- {
- if(s.id==tempId)
- {
- flag=1;
- break;
- }
- }
- break;
- }
- }
- if(flag==1)
- {
- printf("\n\t\tNAME : %s",s.name);
- printf("\n\n\t\tFather's name : %s",s.fname);
- printf("\n\n\t\tMother's name : %s",s.mname);
- printf("\n\n\t\tMobile : %s",s.mobile);
- printf("\n\n\tSGPA: ");
- for(i=0; i<3; i++)
- printf("| %.2f |",s.sgpa[i]);
- printf("\n\n\t\tCGPA : %.2f\n\t",s.cgpa);
- command('-',65);
- }
- else
- printf("\n\n\t\t!!!! ERROR RECORD NOT FOUND !!!!");
- printf("\n\n\t\tWant to enter another search (Y/N)");
- fflush(stdin);
- another=getchar();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement