I have the following persons file:
Name 0744112233 ASD
and the following piece of code
const string InFilePersonRepository::PFILE = "persons";
void InFilePersonRepository::load() {
string delim = " ";
string name, phone, address;
ifstream fin(PFILE.c_str());
while (fin.good()){
fin>>name>>delim>>phone>>delim>>address;
Person p(name,phone,address);
persons.push_back(p);
}
}
After the reading is done, the values of name, phone, address are: name = Name, phone = ASD, address = "",
If the files has multiple lines, same problem, the second field in file is skipped. Why is that happening?
Thanks
>>
skips whitespace, so there is no need for delim
. fin >> name >> phone >> address
should do.
Try something like that
void InFilePersonRepository::load() {
string name, phone, address;
ifstream fin(PFILE.c_str());
while (fin.good()){
fin>>name>>phone>>address;
Person p(name,phone,address);
persons.push_back(p);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.