c - breaking up a preformatted string -



c - breaking up a preformatted string -

i trying code parse line formatted in predetermined fashion.

format: nn nn/nnc... nn 2 digit number should int nnc 2 digit nubmer followed char want maintain string without \ ex. (10 0/0d 1/0d 2/1s 3/3s 4/2s 5/2s 6/5s 7/4s 8/4s 10/9d)

this have code break up

int state = 0; char *tokline; char *buff = "0 0/0d 1/0d 2/1s 3/3s 4/2s 5/2s 6/5s 7/4s 8/4s 10/9d" tokline = strtok( buff, " \n\0"); state = atoi( &tokline[0] ); for( int = 1; < sizeof( tokline ); i++ ){ sscanf( tokline[i], "%i %s", type, instr ); printf("(%i,%i) = %s",state,type,instr); }

but giving me segmentation fault error.

#include <stdio.h> int main(void) { int state, type; char instr[2]; char *tokline; char *buff = "0 0/0d 1/0d 2/1s 3/3s 4/2s 5/2s 6/5s 7/4s 8/4s 10/9d"; char token[7];//7: nn/nnc\0 int len; for(tokline = buff; 1==sscanf(tokline, "%6s%n", token, &len); tokline += len){ int ret = sscanf(token, "%2d/%2d%1s", &state, &type, instr); if(ret == 1) printf("%i\n", state); else //if(ret == 3) printf("(%i,%i) = %s\n",state,type,instr); } homecoming 0; }

c string

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -