I know it is possible to use istringstream
as an alternative to sscanf
, for example:
std::istringstream argtime{"01:00"};
char colon;
int hour;
int minute;
argtime >> std::noskipws;
argtime >> hour >> colon >> minute;
if(argtime && colon == ':') // hh:mm is parsed successfully.
{
// ...
}
The above lines are partly similar to the following sscanf
code:
sscanf("01:00", "%d:%d", &hour, &minute);
But is there any solution to achieve the maximum field width feature of sscanf
via istringstream
in C++ (C++11/C++14/C++1z)?
sscanf("01:00", "%2d:%2d", &hour, &minute);
Edit: I also tried setw
IO manipulator, but it does not work for integers.