Search code examples
c++getline

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


I am very new in C++ and I need some help with the file operation. I am reading a file using getline(), and I when I get a colon, or semicolon etc. I want to store the next word as an element of a class. I used getline() to read from a file and store a specific value as a class element:

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

Solution

  • You can try as below.

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