how to reuse stringstream

前端 未结 6 1150
遇见更好的自我
遇见更好的自我 2021-01-03 22:12

These threads do NOT answer me:

resetting a stringstream

How do you clear a stringstream variable?

        std::ifstream file( szFIleName_p )         


        
6条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-03 22:41

    Imagine a config file.

    par1=11
    par2=22
    

    codes:

    std::string line, strpar1, strpar2;
    int par1, par2;
    std::ifstream configfile("config.cfg");
    
    std::getline(configfile, line);    // first line to variable "line"
    std::istringstream sline(line);
    while (std::getline(sline, strpar1, '='));
    par1 = std::stoi(strpar1);  // par1 get 11
    
    bool b = sline.eof(); // true
    
    std::getline(configfile, line);    // second line to variable "line"
    sline.clear();    //
    sline.str(line);    // reuse "sline"
    
    b = sline.good();  // true  // goodbit is zero, indicating that none of the other bits is set.
    b = sine.fail();  // false
    b = sline.bad();  // false
    b = sline.eof(); // false
    
    while (std::getline(sline, strpar2, '='));
    par2 = std::stoi(strpar2);  // par2 get 22
    

    goodbit is zero, indicating that none of the other bits is set

提交回复
热议问题