Try:
std::getline(ss, s, '='); // gives me "a" std::getline(ss, s); // gives me "b+c" ss.seekg(-(s.size()+1) ,std::ios_base::cur); // Roll back // Need to +1 becuase we throw away a character // with std::getline() that is not in the string. std::getline(ss, s, '+'); // gives me "b"
The problem with the above is that std :: getline () will throw out "\ n" if it finds it before eof. but if he first finds eof, then we have a problem, since +1 puts us in the wrong place. Thus, it works for the above example, but it will fail for the last line if you cannot guarantee that each line ends with the character '\ n'
So, we can use tellg () if you cannot provide this guarantee:
std::getline(ss, s, '='); // gives me "a" std::streampos save = ss.tellg(); std::getline(ss, s); // gives me "b+c" ss.seekg(save); // Roll back std::getline(ss, s, '+'); // gives me "b"
source share