string tokenization
#include <stdio.h> #include <string.h> #include <stdlib.h> int main(int argc, char **argv) { const char * const str = "MAC-:AA-BB-CC-DD-EE-FF"; const char * const delimitation = "-:"; char * const dupstr = strdup(str); char *saveptr = NULL, *substr = NULL; int i = 0; printf("original: %s (%p)\n", dupstr, dupstr); substr = strtok_r(dupstr, delimitation, &saveptr); do { printf("#%d after parsing: %s (%p)\n", i++, substr, substr); substr = strtok_r(NULL, delimitation, &saveptr); } while (substr); } /* [Result] original: MAC-:AA-BB-CC-DD-EE-FF (0x55f73e1e42a0) #0 after parsing: MAC (0x55f73e1e42a0) #1 after parsing: AA (0x55f73e1e42a5) #2 after parsing: BB (0x55f73e1e42a8) #3 after parsing: CC (0x55f73e1e42ab) #4 after parsing: DD (0x55f73e1e42ae) #5 after parsing: EE (0x55f73e1e42b1) #6 after parsing: FF (0x55f73e1e42b4) */
參考資料
沒有留言:
張貼留言