Write a program to create a structure employee having name, id, salary. Create a file “employee.txt” and open in append mode. Write the detail of 5 employee into a file and read the data from the file and display name of employee having highest salary on screen
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
struct employee
{
int id;
std::string name;
int salary;
};
int main()
{
const int EMPLOYEES_COUNT = 5;
const std::string FILE_NAME = "employee.txt";
employee employees[EMPLOYEES_COUNT] =
{
{1, "Robert Baratheon", 10000},
{2, "Jaime Lannister", 3000},
{3, "Cersei Lannister", 5000},
{4, "Daenerys Targaryen", 15000},
{5, "Jorah Mormont", 1000}
};
std::ofstream file1(FILE_NAME, std::ios::app);
if(!file1)
{
std::cerr << "Error: open file 'employee.txt' for appending\n";
return 1;
}
for(int i=0; i<EMPLOYEES_COUNT; ++i)
{
file1 << employees[i].id << " "
<< employees[i].salary << " "
<< employees[i].name << "\n";
}
file1.close();
std::ifstream file2(FILE_NAME);
if(!file2)
{
std::cerr << "Error: open file 'employee.txt' for reading\n";
return 1;
}
employee highestSalaryEmployee = {-1, "", -1};
std::string line;
while(std::getline(file2, line))
{
std::istringstream ist(line);
employee tmp;
ist >> tmp.id >> tmp.salary;
std::getline(ist, tmp.name);
tmp.name = tmp.name.substr(1);
if(tmp.salary > highestSalaryEmployee.salary)
{
highestSalaryEmployee = tmp;
}
}
std::cout << "Name of employee having highest salary: "
<< highestSalaryEmployee.name
<< " (" << highestSalaryEmployee.salary << ")\n";
return 0;
}
Comments
Leave a comment