I have the following Repository
class and I want the initial vector students
to be built from a text file. The objects are of type {string, int, int}
and I'm thinking about using new line as delimiter between objects. Is it be possible to make the private vector be constructed/initialized from file?
Here is the header of my Repository
class:
class StudentRepository{
private:
vector <Student> students;
public:
vector <Student> getAll();
~StudentRepository();
};
P.S.: any advice regarding my question or link to a useful tutorial are welcome.
Edit:
This is were i got but i`m having issues with the loading of the objects because they look like this, how couldi delimit the string from ints?:
Foo bar 100 23
Bar Foo 101 42
Code:
void StudentRepository::loadStudents(){
ifstream fl;
fl.open("studs.txt");
Student A();
if(fl.is_open()){
while(!(fl.eof())){
getline(A.);/// i connot manage to delimite parts of the line.
}
}
else{
cout<<"~~~ File couldn't be open! ~~~"<<endl;
}
}
void StudentRepository::saveStudents(){
ofstream fl;
fl.open("studs.txt");
if(fl.is_open()){
for(int i=0; i<students.size(); i++){
fl<<students[i];
}
}
else{
cout<<"~~~ File couldn't be open! ~~~"<<endl;
}
}