Answer to Question #215996 in C for Nanba

Question #215996

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


1
Expert's answer
2021-07-11T14:39:16-0400
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);
}




Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS