0% found this document useful (0 votes)
11 views3 pages

Menudriven Filehandling

This C program implements a basic employee record keeping system with functions to add, view, search and delete employee records from a text file. The main function displays a menu and calls the appropriate function based on the user's choice.

Uploaded by

okiedokie395
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Menudriven Filehandling

This C program implements a basic employee record keeping system with functions to add, view, search and delete employee records from a text file. The main function displays a menu and calls the appropriate function based on the user's choice.

Uploaded by

okiedokie395
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include<stdio.

h>
#include<process.h>
#include<string.h>

void addrecord();
void viewrecord();
void searchrecord();
void deleterecord();

void main()
{
int choice;
for(;;)
{
printf("\n\n");
printf("Employee Record Keeping System\n");
printf("----------------------------------\n");
printf("1.Add Record\n2.View Record\n");
printf("3.Search Record\n4.Delete Record\n");
printf("5.Exit\nEnter your choice(1-5): ");
scanf("%d",&choice);
switch(choice)
{
case 1:addrecord();
break;
case 2:viewrecord();
break;
case 3:searchrecord();
break;
case 4:deleterecord();
break;
case 5:exit(0);
break;
default: printf("Invalid choice:");
}
}
}
void addrecord()
{
char name[20],address[20];
int salary;
FILE *ptr=fopen("employee.txt","a");
if(ptr==NULL)
{
printf("Unable to open file");
}
else
{
printf("Enter name,address,salary: ");
scanf("%s%s%d",&name,&address,&salary);
fprintf(ptr,"%s %s %d\n",name,address,salary);
fclose(ptr);
}
}

void viewrecord()
{
char name[20],address[20];
int salary;
FILE *ptr=fopen("employee.txt","r");
if(ptr==NULL)
{
printf("Unable to open file");
}
else
{
while(!feof(ptr))
{
fscanf(ptr,"%s %s %d\n",&name,&address,&salary);
printf("%s %s %d\n",name,address,salary);
}

fclose(ptr);
}
}

void searchrecord()
{
char name[20],address[20],name1[20];
int salary;
FILE *ptr=fopen("employee.txt","r");
if(ptr==NULL)
{
printf("Unable to open file");
}
else
{
printf("Enter name to search: ");
scanf("%s",&name1);
while(!feof(ptr))
{
fscanf(ptr,"%s %s %d\n",&name,&address,&salary);
if(strcmpi(name,name1)==0)
printf("%s %s %d\n",name,address,salary);
}

fclose(ptr);
}
}

void deleterecord()
{
char name[20],address[20],name1[20];
int salary;
FILE *ptr=fopen("employee.txt","r");
FILE *ptr1=fopen("temp.txt","w");
if(ptr==NULL||ptr1==NULL)
{
printf("Unable to open file");
}
else
{
printf("Enter name to delete: ");
scanf("%s",&name1);
while(!feof(ptr))
{
fscanf(ptr,"%s %s %d\n",&name,&address,&salary);
if(strcmpi(name,name1)!=0)
fprintf(ptr1,"%s %s %d\n",name,address,salary);
}

fclose(ptr);
fclose(ptr1);
remove("employee.txt");
rename("temp.txt","employee.txt");
}
}

You might also like