I have the following code and I want to build an object. How could I do it? Any ideas? The class is of type {sting,int,int}.
code:
void StudentRepository::loadStudents(){
ifstream fl;
fl.open("studs.txt");
Student A();
if(fl.is_open()){
while(!(fl.eof())){
getline(???); //i dont knwo houw coudl i limit what i want were...
}
}
else{
cout<<"~~~ File couldn't be open! ~~~"<<endl;
}
}
Save to file funcntion:
void StudentRepository::saveStudents(){
ofstream fl;
fl.open("studs.txt");
if(fl.is_open()){
for(unsigned i=0; i<students.size(); i++){
fl<<students[i].getName();
fl<<",";
fl<<students[i].getID();
fl<<",";
fl<<students[i].getGroup();
fl<<","<<endl;
}
}
else{
cout<<"~~~ File couldn't be open! ~~~"<<endl;
}
I tried to implement some limits but that is not working… How canI do this?
Initially I just wrote the object to file but it is harder to get them back to the object….
File content:
maier ewew 123 232
tudor efsw 13 2323
Would overloading the input and output operators for the Student type work for you?
Adding some standard checks for I/O to check that whatever you are reading is valid should do it.