Code:
#include <conio.h>
#include <stdio.h>
/****************************************************************************/
typedef struct student
{
long long int snum;
char password[50];
char lname[31];
char fname[31];
char mi[3];
char bday[7];
char course[6];
}record;
/****************************************************************************/
//DECLARATION OF MAJOR FUNCTIONS
void add_record(void);
void compress(void);
void disp_all(void);
void modifySR(void);
void delSR(void);
void delAll(void);
FILE* fp;
/***************************************************************************/
//MAIN
int main()
{
int menu_choice;
record stud;
if((fp=fopen("std.dat","rb+")) == NULL) {
if((fp = fopen("std.dat", "wb+")) == NULL) {
printf("\n Unable to Open std.dat File - exiting");
getch();
exit(1);
}
}
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 - Compress Records\n");
printf("\n7 - 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) compress();
if(menu_choice==7) {clrscr();
printf("\n\n\nThank you for using SRMS! GoodBye!");}
}while(menu_choice!=7);
fclose(fp);
getch();
return 0;
}
/***************************************************************************/
void add_record()
{
record stud;
char ch;
clrscr();
fseek(fp,0,SEEK_END);
gotoxy(35,1);printf("\n\nADD STUDENT RECORD\n\n");
do
{
printf("\n\nStudent number (must be 4 digits) = ");
scanf("%lld", &stud.snum);
printf("\nPassword = ");
scanf("%49s", stud.password);
printf("\nLast name = ");
scanf("%30s", stud.lname);
printf("\nFirst name = ");
scanf("%30s", stud.fname);
printf("\nMiddle initial = ");
scanf("%2s", stud.mi);
printf("\nBirthdate (MMDDYY) = ");
scanf("%6s", stud.bday);
printf("\nCourse (ex.BSM) = ");
scanf("%5s", stud.course);
printf("\n%lld, %s, %s, %s, %s, %s, %s", stud.snum, stud.password,
stud.lname, stud.fname, stud.mi, stud.bday, stud.course);
fwrite(&stud,sizeof(stud),1,fp);
printf("\n\nAdd another record? Y if yes\n\n");
ch=toupper(getche());
} while(ch=='Y');
getch();
}
/***************************************************************************/
void compress() {
int i, rec_num = 0;
FILE *fp1;
record stud;
rewind(fp);
if((fp1=fopen("new.dat","wb")) == NULL) {
printf("\n Unable to Open new.dat File - exiting");
exit(1);
}
while(fread(&stud, sizeof(stud), 1, fp)) {
if(stud.snum > 0) {
fwrite(&stud, sizeof(stud), 1, fp1);
rec_num++;
}
}
fclose(fp);
fclose(fp1);
if((remove("std.dat")) != 0) {
printf("\nError! Obsolete file: std.dat, was not deleted - exiting\n");
exit(1);
}
else {
rename("new.dat", "std.dat");
if((fp=fopen("std.dat","rb+")) == NULL) {
if((fp = fopen("std.dat", "wb+")) == NULL) {
printf("\n Unable to Open std.dat File - exiting");
getch();
exit(1);
}
}
}
printf("\nThere are currently %d student records\n", rec_num);
getch();
}
void disp_all()
{
record stud;
clrscr();
rewind(fp);
printf("STUDENT#\t%-16s%-16s%-12s%-12s%-12s\n\n","LAST NAME","FIRST NAME","MI","BDAY", "COURSE ");
while(fread(&stud,sizeof(stud),1,fp)) {
if(stud.snum > 0) {
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);
}
}
printf("\n\nPress any key to go back to menu.");
getch();
}
/****************************************************************************/
void modifySR()
{
record stud;
long long int snum;
int x = 0;
fpos_t filepos;
clrscr();
rewind(fp);
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))
{
if(snum==stud.snum)
{
x++;
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);
printf("\n\n\n\nENTER NEW RECORD...\n");
printf("\nStudent number = ");
scanf("%lld", &stud.snum);
printf("\nPassword = ");
scanf("%49s", stud.password);
printf("\nLast name = ");
scanf("%30s", stud.lname);
printf("\nFirst name = ");
scanf("%30s", stud.fname);
printf("\nMiddle initial = ");
scanf("%2s", stud.mi);
printf("\nBirthdate = ");
scanf("%6s", stud.bday);
printf("\nCourse = ");
scanf("%5s", stud.course);
fgetpos(fp, &filepos);
filepos -= sizeof(stud);
fsetpos(fp, &filepos);
fwrite(&stud,sizeof(stud),1,fp);
printf("\n\nRecord modified...");
getch();
}
}
if(x == 0)
printf("\n\nNo record found...");
getch();
}
/***************************************************************************/
void delSR()
{
record stud;
fpos_t filepos;
int ch, j=0;
long long int num;
clrscr();
rewind(fp);
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) )
{
if(num==stud.snum)
{
j++;
printf("\n\nStudent found: \n\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);
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')
{
filepos = ftell(fp);
filepos -= sizeof(stud);
fseek(fp,filepos,SEEK_SET);
stud.snum = -1; //indicating a deleted record
fwrite(&stud,sizeof(stud),1,fp);
printf("\n\nStudent record deleted... ");
}
else
printf("\n\nNo Record Deleted...");
}
}
if(j == 0)
printf("\n\nThat Record Was Not Found...");
getch();
}
/****************************************************************************/
void delAll()
{
record stud;
char ch;
clrscr();
printf("\n\nAre you sure you want to delete all student records?\n\n");
ch=toupper(getche());
if(ch == 'Y')
{
fclose(fp);
printf("\n All student records deleted");
remove("std.dat");
}
else
printf("Press any key to go back to menu.");
getch();
}