-
分号分割,等号解析
#include <iostream> #include <string.h>typedef struct{char name[32];int priority; }codes_t;int codes_get(char* str, codes_t* codes){int i = 0;char *ptr;std::cout<<"before strtok str: "<<str<<std::endl;ptr = strtok(str, ";");while(ptr != NULL){sscanf(ptr, "%*[^=]=%d", &codes[i].priority);sscanf(ptr, "%[^=]", codes[i].name);ptr = strtok(NULL, ";");i++;}return i; } int main() {codes_t codes[32];memset(codes, 0, sizeof(codes_t)*32);char str[]="PCMU/8000/1=130;PCMA/8000/1=0;GSM/8000/1=0;iLBC/8000/1=0;speex/8000/1=0;G722/16000/1=0;speex/16000/1=0;speex/32000/1=0;";int ret = codes_get(str, codes);for(int i = 0; i < ret; i++){std::cout<<"result: "<< codes[i].name << " priority:" << codes[i].priority << std::endl;}return 0;}