简体   繁体   English

用“>>”从文件中读取

[英]Reading from file with “>>”

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 = "", 读完后,姓名,电话,地址的值为: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 . >>跳过空格,所以不需要delim fin >> name >> phone >> address should do. fin >> name >> phone >> address应该做。

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);
            }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM