The file named called EMPLOYEE.txt contains employee details such as employee number, name, designation and salary of employee. Write a program to create a file to store the details of employee (Use of structure and fwrite () & fread () functions)
Runtime Input :
101 xyz workshop 8500
102 nmo computer 6750
103 abc store 5890
Output :
101 xyz workshop 8500
102 nmo computer 6750
103 abc store 5890
using namespace std;
#include <omp.h>
#include <stdio.h>
/*
The file named called EMPLOYEE.txt contains employee details such as employee number, name, designation and salary of employee.
Write a program to create a file to store the details of employee (Use of structure and fwrite () & fread () functions)
Runtime Input :
101 xyz workshop 8500
102 nmo computer 6750
103 abc store 5890
Output :
101 xyz workshop 8500
102 nmo computer 6750
103 abc store 5890
*/
#define NO_OF_EMPLOYEE 3
struct Employee
{
int ID;
string Name;
string Designation;
int Salary;
};
main(void)
{
struct Employee E[NO_OF_EMPLOYEE],R[NO_OF_EMPLOYEE];
int n;
FILE *fpt;
fpt = fpt = fopen(".//Employee.txt","wb");
E[0].ID = 101; E[0].Name="xyz"; E[0].Designation = "workshop"; E[0].Salary=8500;
E[1].ID = 101; E[1].Name="rmo"; E[1].Designation = "computer"; E[1].Salary=6750;
E[2].ID = 101; E[2].Name="abc"; E[2].Designation = "store"; E[2].Salary=5890;
for(n=0;n<NO_OF_EMPLOYEE;n++) fwrite (&E[n], sizeof(Employee), 1, fpt);
fclose(fpt);
fpt = fpt = fopen(".//Employee.txt","rb");
cout<<"\n\tContents read from file:\n";
cout<<"\n\tID Name Designation Salary";
for(n=0;n<NO_OF_EMPLOYEE;n++)
{
fread (&R[n], sizeof(Employee), 1, fpt);
cout<<"\n\t"<<R[n].ID<<"\t"<<setw(10)<<R[n].Name<<"\t"<<setw(10)<<R[n].Designation<<"\t"<<R[n].Salary;
}
fclose(fpt);
return(0);
}
Comments
Leave a comment