Code:
#include<conio.h>
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
/****************************************************************************/
typedef struct student
{
long long int snum;
char password[50];
char fname[31];
char lname[31];
char mi[3];
char bday[7];
char course[6];
}record;
/****************************************************************************/
//DECLARATION OF MAJOR FUNCTIONS
void add_record();
void disp_all();
void modifySR();
void delSR();
void delAll();
/***************************************************************************/
//MAIN
int main()
{
FILE* fp;
int menu_choice;
record stud;
do
{
clrscr();
printf("\n\nADMIN MENU:\n\n");
printf("\n1 - Add student record\n");
printf("\n2 - Display all entries\n");
printf("\n3 - Modify student record\n");
printf("\n4 - Delete student record\n");
printf("\n5 - Delete all entries\n");
printf("\n6 - Log-out and Exit\n");
printf("\nEnter Choice: ");
scanf("%d", &menu_choice);
if(menu_choice==1) {add_record();}
if(menu_choice==2) {disp_all();}
if(menu_choice==3) {modifySR();}
if(menu_choice==4) {delSR();}
if(menu_choice==5) {delAll();}
if(menu_choice==6) {clrscr(); printf("\n\n\nThank you for using SRMS! GoodBye!");}
}while(menu_choice!=6);
getch();
return 0;
}
/***************************************************************************/
void add_record()
{
record stud;
char ch;
FILE *fp;
fp=fopen("std.dat","r+");
if(fp==NULL)
fp=fopen("std.dat","w");
clrscr();
printf("\n\nADD STUDENT RECORD\n\n");
do
{
printf("\n\nStudent number (must be 9 digits) = ");
scanf("%lld", &stud.snum);
printf("\nPassword = ");
scanf("%s", &stud.password);
printf("\nLast name = ");
scanf("%s", &stud.lname);
printf("\nFirst name = ");
scanf("%s", &stud.fname);
printf("\nMiddle initial = ");
scanf("%s", &stud.mi);
printf("\nBirthdate (MMDDYY) = ");
scanf("%s", &stud.bday);
printf("\nCourse (ex.BSM) = ");
scanf("%s", &stud.course);
fseek(fp,0,SEEK_END);
fwrite(&stud,sizeof(stud),1,fp);
printf("\n\nAdd another record? Y if yes\n\n");
ch=toupper(getche());
} while(ch=='Y');
fclose(fp);
getch();
}
/***************************************************************************/
void disp_all()
{
record stud;
FILE *fp;
fp=fopen("std.dat","r+");
if(fp==NULL)
fp=fopen("std.dat","w");
clrscr();
printf("STUDENT#\t%-16s%-16s%-12s%-12s%-12s\n\n","LAST NAME","FIRST NAME","MI","BDAY", "COURSE ");
while(fread( &stud,sizeof(stud),1,fp))
{
printf("%lld\t", stud.snum);
printf("%-16s", stud.lname);
printf("%-16s", stud.fname);
printf("%-12s", stud.mi);
printf("%-12s", stud.bday);
printf("%-12s\n", stud.course);
}
fseek(fp,0,SEEK_END);
printf("\n\nPress any key to go back to menu.");
getch();
}
/****************************************************************************/
void modifySR()
{
record stud;
long long int snum;
int i=0;
int j=0;
int x=0;
FILE *fp;
fp=fopen("std.dat","r+");
if(fp==NULL)
fp=fopen("std.dat","w");
clrscr();
gotoxy(35,2);printf("\n\nMODIFY STUDENT RECORD. Enter student to be modified.\n\n");
printf("Enter student number = ");
scanf("%lld", &snum);
while(fread(&stud,sizeof(stud),1,fp))
{
i++;
if(snum==stud.snum)
{
x++;
j=i-1;
printf("\n\n\n\nORIGINAL RECORD:\n");
printf("\nStudent number = %lld", stud.snum);
printf("\nPassword = %s", stud.password);
printf("\nName = %s, %s %s", stud.lname, stud.fname, stud.mi);
printf("\nBirthdate = %s", stud.bday);
printf("\nCourse = %s", stud.course);
fseek(fp,sizeof(stud)*j,SEEK_SET);
printf("\n\n\n\nENTER NEW RECORD...\n");
printf("\nStudent number = ");
scanf("%lld", &stud.snum);
printf("\nPassword = ");
scanf("%s", &stud.password);
printf("\nLast name = ");
scanf("%s", &stud.lname);
printf("\nFirst name = ");
scanf("%s", &stud.fname);
printf("\nMiddle initial = ");
scanf("%s", &stud.mi);
printf("\nBirthdate = ");
scanf("%s", &stud.bday);
printf("\nCourse = ");
scanf("%s", &stud.course);
fwrite(&stud,sizeof(stud),1,fp);
printf("\n\nRecord modified...");
getch();
}
fseek(fp,sizeof(stud)*i,SEEK_SET);
}
if(x==0)
{
printf("\n\nNo record found...");
getch();
}
fclose(fp);
}
/***************************************************************************/
void delSR()
{
record stud;
int q=0, ch, i, j=0;
int sz;
int tot_rec;
long long int snum;
char password[50];
char fname[31];
char lname[31];
char mi[3];
char bday[7];
char course[6];
long long int snum1;
char password1[50];
char fname1[31];
char lname1[31];
char mi1[3];
char bday1[7];
char course1[6];
long long int num;
FILE *fp,*fp1;
fp=fopen("std.dat","r+");
if(fp==NULL)
fp=fopen("std.dat","w");
fp1=fopen("new.dat","r+");
if(fp1==NULL)
fp1=fopen("new.dat","w");
clrscr();
gotoxy(35,2);printf("\n\nDELETE STUDENT RECORD. Enter student to be deleted.");
printf("\n\nStudent number = ");
scanf("%lld", &num);
while( fread (&stud,sizeof(stud),1,fp) )
{
q++;
if(num==stud.snum)
{
j++;
fseek(fp,sizeof(stud)*(q-1),SEEK_SET);
printf("\n\nStudent found: \n\n");
fread( &stud,sizeof(stud),1,fp);
printf("\nStudent number = %lld", stud.snum);
printf("\nPassword = %s", stud.password);
printf("\nName = %s, %s %s", stud.lname, stud.fname, stud.mi);
printf("\nBirthdate = %s", stud.bday);
printf("\nCourse = %s", stud.course);
printf("\n\n\nAre you sure you want to delete the above student record(s)? Y if yes\n\n");
ch=toupper(getche());
if(ch=='Y')
{
fseek(fp,0,SEEK_END);
sz = ftell(fp);
tot_rec = sz/(sizeof(stud));
rewind(fp);
for(i=0;i<(q-1);i++)
{
fread( &stud,sizeof(stud),1,fp);
snum = stud.snum;
strcpy(password,stud.password);
strcpy(lname,stud.lname);
strcpy(fname,stud.fname);
strcpy(mi,stud.mi);
strcpy(bday,stud.bday);
strcpy(course,stud.course);
stud.snum = snum;
strcpy(stud.password,password);
strcpy(stud.lname,lname);
strcpy(stud.fname,fname);
strcpy(stud.mi,mi);
strcpy(stud.bday,bday);
strcpy(stud.course,course);
fwrite(&stud,sizeof(stud),1,fp1);
}
fseek(fp,sizeof(stud)*(q),SEEK_SET);
for( i=q;i<tot_rec;i++)
{
fread( &stud,sizeof(stud),1,fp);
snum1 = stud.snum;
strcpy(password1,stud.password);
strcpy(lname1,stud.lname);
strcpy(fname1,stud.fname);
strcpy(mi1,stud.mi);
strcpy(bday1,stud.bday);
strcpy(course1,stud.course);
stud.snum = snum1;
strcpy(stud.password,password1);
strcpy(stud.lname,lname1);
strcpy(stud.fname,fname1);
strcpy(stud.mi,mi1);
strcpy(stud.bday,bday1);
strcpy(stud.course,course1);
fwrite(&stud,sizeof(stud),1,fp1);
}
printf("\n\nStudent record deleted... ");
rewind(fp1);
remove("std.dat");
rename("new.dat","std.dat");
getch();
}
else
{
printf("\n\nNo Record Deleted...");
getch();
}
}
}
if(j==0)
{
printf("\n\nNo Record Found...");
}
getch();
}
/****************************************************************************/
void delAll()
{
record stud;
char ch;
FILE *fp,*fp1;
fp=fopen("std.dat","r+");
if(fp==NULL)
fp=fopen("std.dat","w");
fp1=fopen("new.dat","r+");
if(fp1==NULL)
fp1=fopen("new.dat","w");
clrscr();
printf("\n\nAre you sure you want to delete all student records?\n\n");
ch=toupper(getche());
if(ch == 'Y')
{
remove("std.dat");
rename("new.dat","std.dat");
printf("\n\nAll student records deleted...");
getch();
}
else
{
printf("Press any key to go back to menu.");
getch();
}
}
/****************************************************************************/