Skip to content
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
143 lines (129 sloc) 5.64 KB
* A menu-driven program for elementary database management
* @author: Bibek Subedi
* @language: C
* This program uses file handling in Binary mode
/// List of library functions
#include <stdio.h> ///for input output functions like printf, scanf
#include <stdlib.h>
#include <string.h> ///string operations
// Copied from
void flush()
int c;
while ((c = getchar()) != '\n' && c != EOF);
/** Main function started */
int main(){
FILE *fp, *ft; /// file pointers
char another, choice;
/** structure that represent a employee */
struct emp{
char name[40]; ///name of employee
int age; /// age of employee
float bs; /// basic salary of employee
struct emp e; /// structure variable creation
char empname[40]; /// string to store name of the employee
long int recsize; /// 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("Connot open file");
/// sizeo of each record i.e. size of structure variable e
recsize = sizeof(e);
/// infinite loop continues untile the break statement encounter
printf("1. Add Record\n"); /// option for add record
printf("2. List Records\n"); /// option for showing existing record
printf("3. Modify Records\n"); /// option for editing record
printf("4. Delete Records\n"); /// option for deleting record
printf("5. Exit\n"); /// exit from the program
printf("Your Choice: "); /// enter the choice 1, 2, 3, 4, 5
fflush(stdin); /// flush the input buffer
scanf("\n%c", &choice); /// get the input from keyboard
case '1': /// if user press 1
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: ");
fgets(, 40, stdin);
printf("\nEnter age: ");
scanf("%d", &e.age);
printf("\nEnter basic salary: ");
scanf("%f", &;
fwrite(&e,recsize,1,fp); /// write the record in the file
printf("\nAdd another record(y/n) ");
scanf("\n%c", &another);
case '2':
rewind(fp); ///this moves file cursor to start of the file
while(fread(&e,recsize,1,fp)==1){ /// read the file and fetch the record one record per fetch
printf("\n%s %d %.2f\n",,e.age,; /// print the name, age and basic salary
case '3': /// if user press 3 then do editing existing record
another = 'y';
while(another == 'y'){
printf("Enter the employee name to modify: ");
scanf("%s", empname);
while(fread(&e,recsize,1,fp)==1){ /// fetch all record from file
if(strcmp(,empname) == 0){ ///if entered name matches with that in file
printf("\nEnter new name,age and bs: ");
fseek(fp,-recsize,SEEK_CUR); /// move the cursor 1 step back from current position
fwrite(&e,recsize,1,fp); /// override the record
printf("\nModify another record(y/n)");
scanf("\n%c", &another);
case '4':
another = 'y';
while(another == 'y'){
printf("\nEnter name of employee to delete: ");
fgets(empname,40, stdin);
ft = fopen("Temp.dat","wb"); /// create a intermediate file for temporary storage
rewind(fp); /// move record to starting of file
while(fread(&e,recsize,1,fp) == 1){ /// read all records from file
if(strcmp(,empname) != 0){ /// if the entered record match
fwrite(&e,recsize,1,ft); /// move all records except the one that is to be deleted to temp file
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)");
scanf("\n%c", &another);
case '5':
fclose(fp); /// close the file
exit(0); /// exit from the program
return 0;
You can’t perform that action at this time.