#include<stdio.h>
#include<conio.h> void insert_record(); void display_record(); void search_record(); void del_record(); void update_record(); void sort_record(); struct student { int roll; char sec[10]; char name[40]; int marks; float per; }; struct student s; void main() { int ch; clrscr(); while(ch!=7) { clrscr(); printf("\t*********************** HARI OM ****************************\n"); printf("\t************************************************************\n"); printf("\t WELCOME TO STUDENT MANAGEMENT PROJECT"); printf("\n\t*************************************************************\n"); printf("\t\t\t ***AVAILABLE FUNCTIONALTIES***\n"); printf("\t\t1: Insert student record\n"); printf("\t\t2: Display student record\n"); printf("\t\t3: Search student record\n"); printf("\t\t4: Delete student record\n"); printf("\t\t5: Update student record\n"); printf("\t\t6: Sort student record\n"); printf("\t\t7: Exit\n\n"); printf("\t\tEnter your choice:"); scanf("%d",&ch); switch(ch) { case 1: clrscr(); insert_record(); break; case 2: clrscr(); display_record(); break; case 3: clrscr(); search_record(); break; case 4: clrscr(); del_record(); break; case 5: clrscr(); update_record(); break; case 6: clrscr(); sort_record(); break; case 7: exit(1); default: printf("\n\t\tWrong Choice Try Again!!"); } printf("\n\t\tPress Any Key To Continue"); getch(); } } //*****************FUNCTIONS************************** void insert_record() { FILE *fp; fp=fopen("stu.txt","a"); if(fp==NULL) { printf("\n\t\tError: Cannot Open The File!!!"); return; } printf("\t***Previous Stored Data***"); display_record(); printf("\n\n\t***ENTER NEW STUDENT DATA***\n\n"); printf("\n\t\tEnter Student Roll Number:"); scanf("%d",&s.roll); fflush(stdin); printf("\n\t\tEnter Student Name[6-Letter]:"); gets(s.name); printf("\n\t\tEnter Student Section:"); gets(s.sec); printf("\n\t\tEnter Student Total Marks:"); scanf("%d",&s.marks); printf("\n\t\tEnter Student Percentage:"); scanf("%f",&s.per); fwrite(&s,sizeof(s),1,fp); { printf("\n\n\t!!!Student Record Inserted Successfully\n"); } fclose(fp); printf("\n\t\tUpdated Record!!\n"); display_record(); } void display_record() { FILE *fp; fp=fopen("stu.txt","r"); if(fp==NULL) { printf("\n\t\tError :Cannot open the file!!!"); return; } printf("\n\n\t ****Student Details Are As Follows ****\n"); printf("\nRoll.No\t Name \tSection\t\tMarks \tPercentage\n\n"); //printf("a.students roll number\tb.student name\tc.student section\td.student marks\te.student grade while(fread(&s,sizeof(s),1,fp)==1) { printf(" %d \t%s \t%s \t\t%d \t%f\n",s.roll,s.name,s.sec,s.marks,s.per); } fclose(fp); } void search_record() { int ro,flag=0; FILE *fp; fp=fopen("stu.txt","r"); if(fp==NULL) { printf("\n\t\tError: Cannot open the file\n"); return; } printf("\n\n\tEnter Student Roll No Which You Want To Search:"); scanf("%d",&ro); while(fread(&s,sizeof(s),1,fp)>0 && flag==0) { if(s.roll==ro) { flag=1; printf("\n\n\tSearch Successfull And Student Records as Follows\n"); printf("\nRoll.No\tName of Student\t Section\t\tMarks\tPercentage\n"); printf("%d\t%s\t%s \t\t%d\t\t%f\n",s.roll,s.name,s.sec,s.marks,s.per); } } if(flag==0) { printf("\n\n\t\tNo Such Record Found !!!!\n"); } fclose(fp); } void del_record() { char name[40]; unsigned flag=0; FILE *fp,*ft; fp=fopen("stu.txt","r"); ft=fopen("temp.txt","w"); if(fp==NULL) { printf("\n\t\tError: Cannot Open The File!!!"); return; } printf("\n\t *** Previous Stores Data ***"); display_record(); printf("\n\n\tEnter Student Name Which You Want To Delete:"); scanf("%s",name); //ft=fopen("stu1.txt","ab+"); while(fread(&s,sizeof(s),1,fp)==1) { if(strcmp(name,s.name)!=0) { printf("\n\t\tRecord Deleted Successfully\n"); fwrite(&s,sizeof(s),1,ft); } else flag=1; } if(flag==0) { printf("\n\n\t\t No Such Record Found!!!"); } fclose(fp); fclose(ft); remove("stu.txt"); rename("temp.txt","stu.txt"); } void update_record() { int ro,flag=0; FILE *fp; fp=fopen("stu.txt","rb+"); if(fp==NULL) { printf("\n\t\tError: Cannot Open the File!!!"); return; } printf("\n\n\tEnter Roll Number Of Student Whose Record You Want To Update:"); scanf("%d",&ro); printf("\n\t\t*** Previously Stored Record Of Given Roll Number ***"); while(fread(&s,sizeof(s),1,fp)>0 && flag==0) { if(s.roll==ro) { flag=1; printf("\nRoll No\tName of Student\tSection\t\tMarks\tGrade"); printf("%d\t%s\t\t\t\t%s \t\t%d\t\t%f\n",s.roll,s.name,s.sec,s.marks,s.per); printf("\n\t\t*** Now Enter The New Record ***\n\n"); printf("\n\t\tUpdated Student Roll Number:"); scanf("%d",&s.roll); fflush(stdin); printf("\n\t\tUpdated Student Name:"); scanf("%s",&s.name); printf("\n\t\tUpdated Student Section:"); scanf("%s",&s.sec); printf("\n\t\tUpdated Student Marks:"); scanf("%d",&s.marks); printf("\n\t\tUpdated Student Percentage:"); scanf("%f",&s.per); fseek(fp,-(long)sizeof(s),1); fwrite(&s,sizeof(s),1,fp); printf("\n\n\t\tRecord Updated Successfully Cheak the Display"); } } if(flag==0) { printf("\n\t\tError: Something Went Wrong!!!"); } fclose(fp); } void sort_record() { struct student temp; struct student arr[50]; int i,j,k=0; FILE *fp; fp=fopen("stu.txt","rb"); if(fp==NULL) { printf("\n\t\tError: Cannot Open the File!!!"); } i=0; while(fread(&arr[i],sizeof(arr[i]),1,fp)==1) { i++; k++; } for(i=0;i<k;i++) { for(j=0;j<k-i-1;j++) { if(arr[j].roll>arr[j+1].roll) { temp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } printf("\n\t\tAfter Sorting Student Details !!\n\n"); for(i=0;i<k;i++) { printf("%d\t\t%s\t\t%s\t\t%d\t\t%f\n",arr[i].roll,arr[i].name,arr[i].sec,arr[i].marks,arr[i].per); } fclose(fp); }