Reading info from a file

I'm trying to read information from a file and put the data into vectors of its appropriate type. For example: the file will have an int ID, string firstname, string lastname, double salary, int hours_per_week, and string work_position. All this data is separated by ";". I don't know how to read this file into the appropriate vectors: vector<int> ID, vector<string> fname, vector<string> lname, vector<double> salary, vector<int> hpw, vector<string> work. If anyone can help me figure this out, I would be very appreciated.
1) You can use struct so you won't have to deal with dozens of vectors:
1
2
3
4
5
6
7
8
9
struct Employee
{
    int         ID;
    std::string firstname;
    std::string lastname;
    double      salary;
    int         hours_per_week;
    std::string work_position;
};

2) You can use getline() like that:
1
2
3
4
5
6
7
8
std::ifstream input("myfile.data");
std::string temp;
Employee person;
std::getline(input, temp, ';');//Note ';' here. It tells to read file content up to next ; in input
person.ID = std::stoi(temp);
std::getline(input, temp, ';');
person.firstname = temp;
//... And so on 
Last edited on
With struct, how can I make something like an expandable array? Like a vector
1
2
3
4
5
6
std::vector<Employee> foo;
Employee bar {10, "Mke", "Brown", 12.34, 40, "Janitor"};
foo.push_back(bar);
for(auto x: foo)
    std::cout << x.ID << " " << x.firstname << " " << x.lastname<< " " <<
                 x.salary << " " << x.hours_per_week << " " << x.work_position << std::endl;

Last edited on
Ok, I'll see what I can do from here. Thank you.
thank you...
Ok, I'm stuck on the:
1
2
3
4
5
6
std::vector<Employee> foo;
Employee bar {10, "Mke", "Brown", 12.34, 40, "Janitor"};
foo.push_back(bar);
for(auto x: foo)
    std::cout << x.ID << " " << x.firstname << " " << x.lastname<< " " <<
                 x.salary << " " << x.hours_per_week << " " << x.work_position << std::endl;


In my code, I'm asking the user to enter the data for each piece within the struct, then add it to the vector. How does that work exactly?
You can do something like that to let user input data:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
std::vector<Employee> foo;
std::string again;
int Id = 1;
do {
    //Enter employee info
    Employee temp;
    temp.ID = Id;
    std::cout << "Enter first name: ";
    std::cin >> temp.firstname;
    std::cout << "Enter last name: ";
    std::cin >> temp.lastname;
    std::cout << "Enter salary: ";
    std::cin >> temp.salary;
    std::cout << "Enter hours per week: ";
    std::cin >> temp.hours_per_week;
    std::cout << "Enter work position: ";
    std::cin >> temp.work_position;

    foo.push_back(temp); //Adding employee to vector
    ++Id;
    std::cout << "Enter \"Y\" if you want to enter anpther employee info." << std::endl;
    std::cin >> again;
} while(again == "y");

//Outputting data
for(auto x: foo)
    std::cout << x.ID << " " << x.firstname << " " << x.lastname<< " " <<
                 x.salary << " " << x.hours_per_week << " " << x.work_position << std::endl;


dsff
Topic archived. No new replies allowed.