sscanf()probably the easiest option. This is a feature of the C library, so purists may reject it.
Here is an example:
int year;
int month;
int day;
int hour;
int min;
int sec;
const char * str = "2014-06-10 20:05:57";
if (sscanf(str, "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &min, &sec) == 6)
{
}
And here is a live test .
Another nice solution would also be to use C ++ 11 regex , which would make for more robust parsing of the string.
source
share