I have a file, 'staff.txt' that I populate using the function below:
void writeToFile(){
ofstream myFile;
myFile.open("staff.txt",std::ofstream::out | std::ofstream::app);
myFile << staffCode << ",";
myFile << firstName << endl;
myFile.close();
}
The file stores instances of the Staff class:
class Staff{
public:
int staffCode;
string firstName;
string lastName;
void getData(){
cout << "\nPlease enter the following information" << endl;
cout << "Staff Code: ";
cin >> staffCode;
cout << "First Name: ";
cin >> firstName;
cout << "Last Name: ";
cin >> lastName;
}
void showData(){
cout << "Staff Code: " << staffCode << endl;
cout << "Name: " << firstName + " " + lastName << endl;
}
void writeToFile(){
ofstream myFile;
myFile.open("staff.txt",std::ofstream::out | std::ofstream::app);
myFile << staffCode << ",";
myFile << firstName << endl;
myFile.close();
}
};
I would like to read the data in the file into an object array of the Staff class.
This is what I currently have:
int linesInFile = 0;
int i = 0;
string line;
ifstream myFile("staff.txt");
while (getline(myfile, line)){
++linesInFile;
}
Staff myStaff[linesInFile];
if (myFile.is_open()){
while (myFile.good()){
myFile.getline()
}
}