#include<stdio.h>
struct rec
{
int empno;
char name[25],address[50];
float salary;
};
void inputEmployeeData();
int deleteRecord();
void showRecordsInFile();
char fileName[]="Employee.bin";
char tempFilename[]="temp.bin";
int main()
{
inputEmployeeData();
deleteRecord();
showRecordsInFile();
return 0;
}
void inputEmployeeData(){
FILE *fp;
struct rec employee;
puts("\n\t\t\tAdd The Employee Information");
puts("\t\t\t-------------------------------");
fp=fopen(fileName,"wb");
char input='y';
while(input=='y'){
printf ("\nEnter the Employee No.:-> ");
fflush(stdin);
scanf ("%d",&employee.empno);
printf("\nEnter name of Employee:-> ");
fflush(stdin);
gets(employee.name);
printf ("\nEnter Address of Employee:-> ");
fflush(stdin);
gets(employee.address);
printf ("\nEnter Salary of Employee:-> ");
fflush(stdin);
scanf("%f",&employee.salary);
fwrite (&employee,sizeof(employee),1,fp);
printf ("\nRecord Added.");
puts("Do you want to continue ? y/n");
fflush(stdin);
input=getch();
}
fclose(fp);
}
/*
To remove a record from a file we copy its records, except the one to be deleted, to a new
file.
*/
int deleteRecord(){
struct rec employee;
FILE *fp;
puts("\n\t\t\tDelete Employee Record");
puts("\t\t\t-------------------------------");
FILE *copy;
char employeeName[25];
char tempFilename[]="temp.bin";
printf("Please enter name of employee whom record you want to delete :");
fflush(stdin);
gets(employeeName);
fp = fopen(fileName, "rb");
copy = fopen(tempFilename, "wb");
fread(&employee, sizeof(employee), 1, fp);
while (!feof(fp)) {
//copy its records, except the one to be deleted
if (strcmp(employee.name,employeeName) != 0)
fwrite(&employee, sizeof(employee), 1, copy);
fread(&employee, sizeof(employee), 1, fp);
}
fclose(fp);
fclose(copy);
//remove the old file and give the new file this name
remove(fileName);
int result=rename(tempFilename,fileName);
if ( result == 0 )
puts ( "File successfully renamed" );
else
perror( "Error renaming file" );
return 0;
}
void showRecordsInFile(){
int counter=0;
puts("\n\t\t\tAll Employees Records");
puts("\t\t\t-------------------------------");
struct rec employee;
FILE* fp;
fp=fopen(fileName,"r");
if (fp==NULL)
{
printf("\nUnable to open file");
getch();
return ;
}
//read all records from file
fread(&employee,sizeof(employee),1,fp);
while (!feof(fp)) {
counter++;
printf("%d) %d\t%s\t%s\t%f\n",counter,employee.empno,employee.name,employee.address,employee.salary);
fread(&employee,sizeof(employee),1,fp);
}
getch();
fclose(fp);
}
struct rec
{
int empno;
char name[25],address[50];
float salary;
};
void inputEmployeeData();
int deleteRecord();
void showRecordsInFile();
char fileName[]="Employee.bin";
char tempFilename[]="temp.bin";
int main()
{
inputEmployeeData();
deleteRecord();
showRecordsInFile();
return 0;
}
void inputEmployeeData(){
FILE *fp;
struct rec employee;
puts("\n\t\t\tAdd The Employee Information");
puts("\t\t\t-------------------------------");
fp=fopen(fileName,"wb");
char input='y';
while(input=='y'){
printf ("\nEnter the Employee No.:-> ");
fflush(stdin);
scanf ("%d",&employee.empno);
printf("\nEnter name of Employee:-> ");
fflush(stdin);
gets(employee.name);
printf ("\nEnter Address of Employee:-> ");
fflush(stdin);
gets(employee.address);
printf ("\nEnter Salary of Employee:-> ");
fflush(stdin);
scanf("%f",&employee.salary);
fwrite (&employee,sizeof(employee),1,fp);
printf ("\nRecord Added.");
puts("Do you want to continue ? y/n");
fflush(stdin);
input=getch();
}
fclose(fp);
}
/*
To remove a record from a file we copy its records, except the one to be deleted, to a new
file.
*/
int deleteRecord(){
struct rec employee;
FILE *fp;
puts("\n\t\t\tDelete Employee Record");
puts("\t\t\t-------------------------------");
FILE *copy;
char employeeName[25];
char tempFilename[]="temp.bin";
printf("Please enter name of employee whom record you want to delete :");
fflush(stdin);
gets(employeeName);
fp = fopen(fileName, "rb");
copy = fopen(tempFilename, "wb");
fread(&employee, sizeof(employee), 1, fp);
while (!feof(fp)) {
//copy its records, except the one to be deleted
if (strcmp(employee.name,employeeName) != 0)
fwrite(&employee, sizeof(employee), 1, copy);
fread(&employee, sizeof(employee), 1, fp);
}
fclose(fp);
fclose(copy);
//remove the old file and give the new file this name
remove(fileName);
int result=rename(tempFilename,fileName);
if ( result == 0 )
puts ( "File successfully renamed" );
else
perror( "Error renaming file" );
return 0;
}
void showRecordsInFile(){
int counter=0;
puts("\n\t\t\tAll Employees Records");
puts("\t\t\t-------------------------------");
struct rec employee;
FILE* fp;
fp=fopen(fileName,"r");
if (fp==NULL)
{
printf("\nUnable to open file");
getch();
return ;
}
//read all records from file
fread(&employee,sizeof(employee),1,fp);
while (!feof(fp)) {
counter++;
printf("%d) %d\t%s\t%s\t%f\n",counter,employee.empno,employee.name,employee.address,employee.salary);
fread(&employee,sizeof(employee),1,fp);
}
getch();
fclose(fp);
}