Untitled
unknown
plain_text
a year ago
2.6 kB
3
Indexable
#include <stdio.h>
#include <stdlib.h>
struct employee {
int id;
char name[25];
char dept[25];
int sal;
int age;
};
void read(struct employee e[],int size)
{
for(int i=0;i<size;i++)
{
printf("\nDetails of Employee %d:",i+1);
printf("\nEnter an Employee id: ");
scanf("%d",&e[i].id);
printf("Enter an Employee Name: ");
scanf("%s",e[i].name);
printf("Enter an Employee Deptartment {IN CAPS}: ");
scanf("%s",e[i].dept);
printf("Enter an Employee Salary: ");
scanf("%d",&e[i].sal);
printf("Enter an Employee age: ");
scanf("%d",&e[i].age);
}
}
void displayAll(struct employee e[],int size,FILE *ptr) {
for(int i=0;i<size;i++) {
printf("Details of Employee %d:\n",i+1);
printf("Id:%d\nName:%s\nDepartment:%s\nSalary:%d\nAge:%d\n",e[i].id,e[i].name,e[i].dept,e[i].sal,e[i].age);
fprintf(ptr,"Details of Employee %d:\n",i+1);
fprintf(ptr,"Id:%d\nName:%s\nDepartment:%s\nSalary:%d\nAge:%d\n",e[i].id,e[i].name,e[i].dept,e[i].sal,e[i].age);
}
printf("Result written to 'employeeRecord.txt' is successful.\n");
}
void search(struct employee e[],char department[],int size,FILE *ptr) {
for(int i=0;i<size;i++)
{
if(strcmp(department,e[i].dept)==0) {
fprintf(ptr,"Details of Employee %d:\n",i+1);
fprintf(ptr,"Id:%d\nName:%s\nDepartment:%s\nSalary:%d\nAge:%d\n",e[i].id,e[i].name,e[i].dept,e[i].sal,e[i].age);
printf("Details of Employee %d:\n",i+1);
printf("Id:%d\nName:%s\nDepartment:%s\nSalary:%d\nAge:%d\n",e[i].id,e[i].name,e[i].dept,e[i].sal,e[i].age);
printf("Result written to 'employeeRecord.txt' is successful.\n");
}
else
printf("Entered Department is not found.\n");
}
}
int main()
{
FILE *ptr;
int n;
printf("Enter the Number of employee:");
scanf("%d",&n);
struct employee e[n];
read(e,n);
ptr=fopen("employeeRecord.txt","w+");
if(ptr==NULL) {
printf("Error in opening file!\n");
return 1;
}
int choice;
printf("If you want to display all Employee Record Enter 1 Else 2 for search employee by Department:");
scanf("%d",&choice);
if(choice==1)
displayAll(e,n,ptr);
else if(choice==2) {
char department[25];
printf("\nEnter the Department {IN CAPS}: ");
scanf("%s",department);
search(e,department,n,ptr);
}
fclose(ptr);
return 0;
}
Editor is loading...
Leave a Comment