Say I have a textfile "Employees.txt" with Employee name and ID.
Like so:
John:d4250
Sarah:s5355
Alan:r4350
If I have a very very basic Employee class which has a constructor for Name and ID and I wish to read from this textfile and insert them into a vector
Am I best to use something like:
void GenericProgram::loadEmployees()
{
string line;
ifstream empFile("employees.txt");
if(empFile.fail())
{
cout << "input file opening failed\n" << endl;
exit(EXIT_FAILURE);
}
while (!empFile.eof())
{
string empName;
string empID;
while (getline(empFile, line, '\n'))
{
// this will give me the line on its own
// now how to delimit again using ':'
// then do something like
Employee e(empName, empID)
employeeVector.push_back(e);
}
empFile.close();
}
}
I apologise that this is so basic. Brain is failing on me. I was wondering if there are better ways to read from files to populate objects with streams.