Program to manipulate and
store employee records
/*
* problem 3
* employee record program
*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#define NILL 0
#define Set_NULL(employeeRec) (*((unsigned long long int*)&employeeRec) = NILL)
#define Check_If_Null(employeeRec) ( *((unsigned long long int*)&employeeRec) == NILL )
struct EMPLOYEE;
typedef struct EMPLOYEE Employee;
struct EMPLOYEE
{
char name[50];
float Salary;
char designation[100];
unsigned int empNo;
};
Employee getRecord()
{
Employee emp;
printf("\nenter the employee details: ");
printf("\nenter name: "); fflush(stdin);
gets(emp.name); fflush(stdin);
printf("\nenter the salary of the employee: ");
scanf("%f", &emp.Salary); fflush(stdin);
printf("\nenter the designation of the employee: ");
scanf("%s", emp.designation); fflush(stdin);
printf("\nenter the employee number: ");
scanf("%d", &emp.empNo);
return emp;
}
void displayRecord(Employee emp)
{
if (Check_If_Null(emp))
{
fprintf(stderr, "\nerror: record not found");
return;
}
printf("\nname: %s", emp.name);
printf("\nsalary: %f", emp.Salary);
printf("\nemployee number: %d", emp.empNo);
printf("\nemployee designation: %s", emp.designation);
}
int WriteRecordToFile(Employee emp, const char* F_name)
{
FILE *f;
f = fopen(F_name, "r+b");
if (f == NULL)
{
f = fopen(F_name, "w+b");
if (f == NULL)
{
fprintf(stderr, "error: unable to open the file %s", F_name);
getch();
return -1;
}
}
fseek(f, 0, SEEK_END);
fwrite(&emp, sizeof(emp), 1, f);
fclose(f);
return 0;
}
Employee FindRecord(const char* name, const char* F_name)
{
Employee emp;
FILE *f = fopen(F_name, "rb");
if (f == NULL)
{
fprintf(stderr, "error: unable to open file");
Set_NULL(emp);
return emp;
}
while (!feof(f))
{
fread(&emp, sizeof(emp), 1, f);
if (strcmp(name, emp.name) == NILL)
{
fclose(f);
return emp;
}
}
Set_NULL(emp);
fclose(f);
return emp;
}
void updateSalary(const char* Name, const char* F_Name, float salary)
{
Employee emp;
FILE *f = fopen(F_Name, "r+b");
while (!feof(f))
{
fread(&emp, sizeof(emp), 1, f);
if (strcmp(Name, emp.name) == NILL)
{
emp.Salary = salary;
fseek(f, -((int)sizeof(emp)), SEEK_CUR);
fwrite(&emp, sizeof(emp), 1, f);
fclose(f);
return;
}
}
fclose(f);
}
void displayMenu()
{
printf("\n1. add record");
printf("\n2. find record by name");
printf("\n3. modify salary");
printf("\n4. exit ");
}
int main()
{
Employee tempEmp; char ch = '\0';
char name[50]; float Salary;
while (ch != '4')
{
system("cls");
displayMenu();
ch = getch();
switch (ch)
{
case '1':
tempEmp = getRecord();
WriteRecordToFile(tempEmp, "records.dat");
break;
case '2':
printf("\nenter the name:");
fflush(stdin);
gets(name);
tempEmp = FindRecord(name, "records.dat");
displayRecord(tempEmp);
getch();
break;
case '3':
printf("\nenter the employee name: ");
fflush(stdin);
gets(name);
tempEmp = FindRecord(name, "records.dat");
if (Check_If_Null(tempEmp))
{
getch();
continue;
}
printf("enter salary: ");
fflush(stdin);
scanf("%f", &Salary);
updateSalary(name, "records.dat", Salary);
break;
case '4': break;
default:
fprintf(stderr, "error: invalid choice");
getch();
}
}
return 0;
}
copyright (c) 2015 K Sreram.
About my blog
No comments:
Post a Comment