我有衰减功能,我不明白为什么它不起作用。保存后,数据被正确保存,但在读取时,它不会读取int部分。
void StudentRepository::loadStudents(){
ifstream fl;
fl.open("studs.txt");
Student st("",0,0);
string str,s;
stringstream ss;
int i;
int loc;
if(fl.is_open()){
while(!(fl.eof())){
getline(fl,str);
loc = str.find(",");
ss << str.substr(0,loc);
s = ss.str();
st.setName(s);
str.erase(0,loc);
loc = str.find(",");
ss << str.substr(0,loc);
ss >> i;
st.setId(i);
str.erase(0,loc);
ss >> i;
st.setGroup(i);
students.push_back(st);
}
}
else{
cout<<"~~~ File couldn t be open! ~~~"<<endl;
}
fl.close();
}
编辑
class Student {
private:
string name;
int ID;
int group;
public:
Student(string name, int id, int gr ):name(name),ID(id),group(gr){}
void setId(int value) {group = value;}
void setGroup(int value) {ID = value;}
void setName(string value) {name = value;}
int getGroup() const{return group;}
int getID() const{return ID;}
string getName() const{return name;}
friend ostream& operator << (ostream& out, const Student& student)
{
out << student.name << " " << student.ID << " " << student.group <<endl;
return out;
}
};
file:(在打印时,当我在load函数中初始化对象时,所有int都是0)
maier tsdar,2,0
staedfsgu aldasn,0,3
pdasdp aasdela,323,23
marsdaciu baleen,234,4534
madsd,234,2345