How to clear width when outputting from a stream, after using std::setw?
Remember that the input operator >>
stops reading at whitespace.
Use e.g. std::getline
to get the remainder of the string:
std::stringstream ss("123ABCDEF1And then the rest of the string");
ss >> std::setw(3) >> nId
>> std::setw(6) >> sLabel
>> std::setw(1) >> bFlag;
std::getline(ss, sLeftovers);
std::setw
only affects exactly one operation, i.e. >> bFlag
will reset it to default, so you don't need to do anything to reset it.
i.e. your code should just work
std::stringstream ss("123ABCDEF1And then the rest of the string");
ss >> std::setw(3) >> nId
>> std::setw(6) >> sLabel
>> std::setw(1) >> bFlag
>> sLeftovers;