Saturday, 15 August 2015

C++ stringstream tellg() -



C++ stringstream tellg() -

i'm writing little parser in c++98 (yup, cannot utilize 11). i'm working std::stringstream pass reference different functions, let's phone call them subparsers. in order know subparser phone call need know next word in stringstream. stringstream istream have peek function returns next character without moving iterator / pointer / whatever marks current location within stringstream, need next word wrote function peekword (ignore commented line now):

std::string parser::peekword(std::stringstream& sstream){ std::string mystring = "eof"; if(!sstream.eof()){ unsigned pos = sstream.tellg(); sstream >> mystring; //sstream.tellg(); sstream.seekg(pos); } homecoming mystring; }

which seems work nicely. while debugging noticed, phone call tellg() after pointer/marker/thing has been moved past final word (which returns -1), seekg(xbeforelastposition) doesn't work anymore , still sets position -1.

does phone call of tellg() @ end of stringstream set failbit or that? intuitively had hoped void function tellg() has no side effects.

looking forwards hearing guys :)

pip

tellg specified such:

returns: after constructing sentry object, if fail() != false, returns pos_type(-1) indicate failure. otherwise, returns rdbuf()->pubseekoff(0, cur, in).

(istream::sentry objects used check input available.)

so, yes, set failbit on eof. can observe checking eof() , using clear() homecoming normal processing.

c++ stringstream

No comments:

Post a Comment