Skip to content

Instantly share code, notes, and snippets.

@dan0nchik
Created March 24, 2023 11:16
Show Gist options
  • Save dan0nchik/9f858ae253f83d177723e7ed2f24aad1 to your computer and use it in GitHub Desktop.
Save dan0nchik/9f858ae253f83d177723e7ed2f24aad1 to your computer and use it in GitHub Desktop.
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
struct Employee {
std::string name;
std::vector<int> salaries;
};
std::istream& fillLine(std::istream& file, Employee& employee)
{
std::string line;
std::getline(file, line);
// std::cout << line << std::endl;
std::stringstream ss(line);
std::string name;
std::getline(ss, name, '\t');
std::vector<int> salaries;
std::string eachSalary;
while (std::getline(ss, eachSalary, '\t'))
{
salaries.push_back(std::stoi(eachSalary));
}
employee.name = name;
employee.salaries = salaries;
return file;
}
void fillEmployees(std::istream& file, std::vector<Employee>& employees)
{
std::string buffer;
std::getline(file, buffer);
Employee employee;
while (fillLine(file, employee))
{
employees.push_back(employee);
}
}
std::ostream& operator<<(std::ostream& out, const Employee& employee)
{
out << employee.name << std::endl;
for (auto salary: employee.salaries)
{
out << salary << " ";
}
out << std::endl;
return out;
}
int main()
{
std::string path = "/Users/danonchik/CLionProjects/contest/employees.csv";
std::ifstream file;
file.open(path);
std::vector<Employee> employees;
if (file)
{
fillEmployees(file, employees);
}
for (auto employee: employees)
{
std::cout << employee;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment