parsing - How to parse simple string in C? -
i have string following format:
start 123
i parsing way:
if (strstr(line, "start") == line) { int number = -1; if (sscanf(line + strlen("start "), "%d", &number) == 1) { printf("start %d\n", number); } }
is there better way in c?
yes can use this:
if (sscanf(line, "start %d", &number ) == 1) {
no need
if (strstr(line, "start") == line) {
any more
if want check more: check there no characters after number , can use following format:
char c; if (sscanf(line, "start %d%c", &number, &c) == 1) {
note: above formats (and yours) not check there 1 space between "start" , number. if string "start \t 45"
if
return true
Comments
Post a Comment