Answer to Question #180160 in C++ for Kushal Sharma

Question #180160

Create Create a class Employee with the data members such as name, id and total salary. Create a file as “Employee_Data” as binary file and write the information of four employees to that file. Open that file in reading mode and only display the record of 2nd and 3rd employees.


1
Expert's answer
2021-04-10T15:31:35-0400
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

class Employee
{
	std::string name;
	int         id;
	double      salary;

  public:

	Employee(const std::string& Name, int ID, double Salary)
			:name(Name), id(ID), salary(Salary)
	{
	}

	std::string GetName()   const { return name; }
	int         GetID()     const { return id; }
	double      GetSalary() const { return salary; }
};

void WriteCount(std::ofstream& file, size_t count)
{
	file.write(reinterpret_cast<const char*>(&count), sizeof(count));	
}

void WriteEmployee(std::ofstream& file, const Employee& employee)
{
	std::string nameData(employee.GetName());
	size_t nameSize = nameData.size();
	int id = employee.GetID();
	double salary = employee.GetSalary();
	
	file.write(reinterpret_cast<const char*>(&nameSize), sizeof(nameSize));
	file.write(nameData.c_str(),                         nameData.size());
	file.write(reinterpret_cast<const char*>(&id),       sizeof(id));
	file.write(reinterpret_cast<const char*>(&salary),   sizeof(salary));
}

size_t ReadCount(std::ifstream& file)
{
	size_t count;
	file.read(reinterpret_cast<char*>(&count), sizeof(size_t));
	return count;
}

Employee ReadEmployee(std::ifstream& file)
{
	size_t nameSize;
	int id;
	double salary;

	file.read(reinterpret_cast<char*>(&nameSize), sizeof(nameSize));
	std::vector<char> nameBuffer(nameSize);
	file.read(&nameBuffer[0], nameBuffer.size());
	file.read(reinterpret_cast<char*>(&id),     sizeof(id));
	file.read(reinterpret_cast<char*>(&salary), sizeof(salary));

	return Employee(std::string(nameBuffer.begin(), nameBuffer.end()), id, salary);
}

int main()
{
	std::vector<Employee> employees;
	employees.push_back(Employee("Robert Baratheon",   1, 4000.00));
	employees.push_back(Employee("Jaime Lannister",    2, 1000.00));
	employees.push_back(Employee("Cersei Lannister",   3, 3000.00));
	employees.push_back(Employee("Daenerys Targaryen", 4, 5000.00));

	std::ofstream outFile("Employee_Data", std::ios::binary);

		if(!outFile)
		{
			std::cerr << "Open file for writing failed\n";
			return 1;
		}

	WriteCount(outFile, employees.size());
	for(size_t i=0; i<employees.size(); ++i)
	{
		WriteEmployee(outFile, employees[i]);
	}

	outFile.close();
	
	std::ifstream inFile("Employee_Data", std::ios::binary);

		if(!inFile)
		{
			std::cerr << "Open file for reading failed\n";
			return 1;
		}

	size_t count = ReadCount(inFile);
	std::vector<Employee> employees2;
	for(size_t i=0; i<count; ++i)
	{
		employees2.push_back(ReadEmployee(inFile));
	}

	if(employees2.size() < 3)
	{
        std::cerr << "Unexpected number of records in file\n";
		return 1;		
	}

	std::cout << "Employee #"   << employees2[1].GetID()
			  << ": Name = '"   << employees2[1].GetName()
			  << "', Salary = " << employees2[1].GetSalary() << "\n";
	std::cout << "Employee #"   << employees2[2].GetID()
			  << ": Name = '"   << employees2[2].GetName()
			  << "', Salary = " << employees2[2].GetSalary() << "\n";
	
    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