Employ Details File Handling coderinme

Employ Details File Handling coderinme

Let us write a menu driven program to perform following operations:
First of all, Append the data of a new employee joining the firm.
Next, Delete record if employee leaves.
Also, Update the gross salary.
Display the records.

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
struct emp
{
int emp_no;
char name[40];
int age;
float bs;
};
int main()
{
FILE *outp,*inpt;
char another,choice;
struct emp e;
char empname[40];
long int recsize;
outp = fopen("emp.txt","r+");
if(outp == NULL)
{
outp = fopen("emp.txt","w+");
if(outp == NULL)
{
puts("cannot open file\n");
exit(1);
}
}
recsize = sizeof(e);
while(1)
{
printf("\n1.Add records\t2.List records\t3.Modify records\n4.Delete records\t0. exit\n");
printf("Your choice\n");
fflush(stdin);
choice = getche();
switch(choice)
{
case '1':
printf("\n");
fseek(outp,0,SEEK_END);
another='Y';
while(another=='Y')
{
printf("\nEnter emp_number name,age and gross salary\n");
scanf("%d %s %d %f",&e.emp_no,&e.name,&e.age,&e.bs);
fwrite(&e,recsize,1,outp);
printf("Add another record(Y/N): ");
fflush(stdin);
another=getche();
}
break;
case '2':
printf("\n");
rewind(outp);
while(fread(&e,recsize,1,outp)==1)
printf("\n%d %s %d %f\n",e.emp_no,e.name,e.age,e.bs);
break;
case '3':
printf("\n");
another='Y';
while(another=='Y')
{
printf("\nEnter name of employee to modify");
scanf("%s",empname);
rewind(outp);
while(fread(&e,recsize,1,outp)==1)
{
if(strcmp(e.name,empname)==0)
{
printf("\nenter new name,age & gs");
scanf("%d %s %d %f",&e.emp_no,&e.name,&e.age,&e.bs);
fseek(outp,-recsize,SEEK_CUR);
fwrite(&e,recsize,1,outp);
break;
}
}
printf("\nModify another record(Y/N): ");
fflush(stdin);
another=getche();
}
break;
case '4':
printf("\n");
another='Y';
while(another=='Y')
{
printf("\nEnter name of employee to delete\n");
scanf("%s",empname);
inpt=fopen("temp.txt","wb");
rewind(outp);
while(fread(&e,recsize,1,outp)==1)
{
if(strcmp(e.name,empname)!=0)
fwrite(&e,recsize,1,inpt);
}
fclose(outp);
fclose(inpt);
remove("emp.txt");
rename ("temp.txt","emp.txt");
outp=fopen("emp.txt","rb+");
printf("Delete another record(Y/N): \n");
fflush(stdin);
another=getche();
}
break;
case '0':
fclose(outp);
getch();
exit(0);
}
}
return 0;
}

For more programs in C, visit our C Coder Archives

Employ Details File Handling coderinme

All rights reserved. No part of this Post may be copied, distributed, or transmitted in any form or by any means, without the prior written permission of the website admin, except in the case of brief quotations embodied in critical reviews and certain other noncommercial uses permitted by copyright law. For permission requests, write to the owner, addressed “Attention: Permissions Coordinator,” to the admin @coderinme

Leave a reply:

Your email address will not be published.