Untitled
unknown
plain_text
2 years ago
7.4 kB
7
Indexable
#include <stdio.h> ///for input output functions like printf, scanf #include <stdlib.h> #include <conio.h> #include <string.h> ///string operations #include "navigation.h" #include "user_interface.h" int main() { displayUserInterface(); FILE *fp, *ft; /// file pointers char another, choice; /** structure that represent a employee */ struct employee { char name[40]; int age; float basicSalary; }; struct employee activeEmployee; /// structure variable creation char employeeName[40]; /// string to store name of the employee long int recordSize; /// size of each record of employee /** open the file in binary read and write mode * if the file EMP.DAT already exists then it open that file in read write mode * if the file doesn't exit it simply create a new copy */ fp = fopen("EMP.DAT","rb+"); if(fp == NULL) { fp = fopen("EMP.DAT","wb+"); if(fp == NULL) { printf("Cannot open file"); exit(1); } } /// sizeo of each record i.e. size of structure variable activeEmployee recordSize = sizeof(activeEmployee); /// infinite loop continues until the break statement encounter while(1) { fflush(stdin); /// flush the input buffer choice = getche(); /// get the input from keyboard switch(choice) { case '1': /// if user press 1 system("cls"); fseek(fp,0,SEEK_END); /// search the file and move cursor to end of the file /// here 0 indicates moving 0 distance from the end of the file another = 'y'; while(another == 'y') /// if user want to add another record { printf("\nEnter name: "); scanf("%s",activeEmployee.name); printf("\nEnter age: "); scanf("%d", &activeEmployee.age); printf("\nEnter basic salary: "); scanf("%f", &activeEmployee.basicSalary); fwrite(&activeEmployee,recordSize,1,fp); /// write the record in the file printf("\nAdd another record(y/n) "); fflush(stdin); another = getche(); } break; case '2': system("cls"); rewind(fp); ///this moves file cursor to start of the file while(fread(&activeEmployee,recordSize,1,fp)==1) /// read the file and fetch the record one record per fetch { printf("\n%s %d %.2f",activeEmployee.name,activeEmployee.age,activeEmployee.basicSalary); /// print the name, age and basic salary } getch(); break; case '3': /// if user press 3 then do editing existing record system("cls"); another = 'y'; while(another == 'y') { printf("Enter the employee name to modify: "); scanf("%s", employeeName); rewind(fp); while(fread(&activeEmployee,recordSize,1,fp)==1) /// fetch all record from file { if(strcmp(activeEmployee.name,employeeName) == 0) ///if entered name matches with that in file { printf("\nEnter new name,age and bs: "); scanf("%s%d%f",activeEmployee.name,&activeEmployee.age,&activeEmployee.basicSalary); fseek(fp,-recordSize,SEEK_CUR); /// move the cursor 1 step back from current position fwrite(&activeEmployee,recordSize,1,fp); /// override the record break; } } printf("\nModify another record(y/n)"); fflush(stdin); another = getche(); } break; case '4': system("cls"); another = 'y'; while(another == 'y') { printf("\nEnter name of employee to delete: "); scanf("%s",employeeName); ft = fopen("Temp.dat","wb"); /// create a intermediate file for temporary storage rewind(fp); /// move record to starting of file while(fread(&activeEmployee,recordSize,1,fp) == 1) /// read all records from file { if(strcmp(activeEmployee.name,employeeName) != 0) /// if the entered record match { fwrite(&activeEmployee,recordSize,1,ft); /// move all records except the one that is to be deleted to temp file } } fclose(fp); fclose(ft); remove("EMP.DAT"); /// remove the orginal file rename("Temp.dat","EMP.DAT"); /// rename the temp file to original file name fp = fopen("EMP.DAT", "rb+"); printf("Delete another record(y/n)"); fflush(stdin); another = getche(); } break; case '5': fclose(fp); /// close the file exit(0); /// exit from the program } } return 0; }
Editor is loading...