如何根据 C++ 中给出的信息在文件中使用 getline() 读取下一个单词?

How to read the next word using getline() in a file based on an information given in C++?

我是 C++ 的新手,需要一些文件操作方面的帮助。我正在使用 getline() 读取文件,当我得到一个冒号或分号等时,我想将下一个单词存储为 class 的一个元素。我使用 getline() 从文件中读取并将特定值存储为 class 元素:

void read(string f){
    string s;
    ifstream fin;
    fin.open(f.c_str());

    configFile c;

    while(fin){
        getline(fin, s, ' '); //reading from file, terminates when finds space
        s = small(s); // converting to lower case
        if (s = "version/phase:"){
            c.ver = getline() // want to store the next word in c.ver
        }
    }
}

您可以尝试以下方法。

void read(string f){
string s;
ifstream fin;
fin.open(f.c_str());

configFile c;

while(fin>>s){
    //getline(fin, s, ' '); //reading from file, terminates when finds space
    s = small(s); // converting to lower case
    if (s == "version/phase:"){
        fin>>s;
        c.ver = s;// getline() // want to store the next word in c.ver
    }
  }
}