12-06-2010 09:44 AM
I have a long text string that gets parsed as I create custom controls. The issue I am having is having a percent sign in this text string and flags are getting thrown.
I first doubled the percent sign which removed my first error of an unknown specifier. Now, i am getting it again, but later in my code.
string = "ring:Select Variable to Check-Var 1=1,Var 2=2,Var 3=3;ring:Variable Type Compare-char=1,int=2,float/double=3,string=4;value:Format specifier by using %%v (used in case value returned is within string. see help)-%%v;ring:Operation-Less Than=1,Equal To=2,Greater Than=3,Not Equal to=4;value:Value to compare-"
string_args = malloc(strlen(string_arguments)+1); sprintf(string_args,string_arguments); //errored here first time token = strtok(string_args,";"); while (token != NULL) { item_list=0; arg_token = realloc(arg_token,strlen(token)+1); sprintf(arg_token,token); //error here now arg_ptr = strtok(arg_token,":"); if (strstr(arg_ptr,"ring") != NULL) { arg_ptr = strtok(NULL,"-"); //should now hold title of ring controlIDs[i] = NewCtrl(funcHandle,CTRL_RING,arg_ptr,50*(i+1)+offset,40); SetCtrlAttribute(funcHandle,controlIDs[i],ATTR_WIDTH,200); while (arg_ptr != NULL) { arg_ptr = strtok(NULL,"="); if ((arg_ptr == NULL) || (strchr(arg_ptr,';') != NULL)) break; value = atoi(strtok(NULL,",\0")); //holds value InsertListItem(funcHandle,controlIDs[i],-1,arg_ptr,value); if (arg_ptr[0] == '*') { GetNumListItems(funcHandle,controlIDs[i],&item_list); //holds index-1 to set active } } } //Rest of if/elses not supplied since I know looping works i++; sprintf(string_args,string_arguments); //resets tokening token = strtok(string_args,";"); for (int j = 0; j < i; j++) { token = strtok(NULL,";"); }
It's almost like the extra percent signs got stripped the first time and the second time i process that string, they error.
Solved! Go to Solution.
12-06-2010 10:11 AM
In the example you posted, it seems as though you could use strcpy() instead of sprintf(). strcpy() would not attempt to interpret the contents of the string in the same way.
JR
12-06-2010 10:13 AM
Well found my answer pretty quickly after some ivnestigation and apparently my laziness caught up with me.
All of my sprintf statements should be like:
sprintf(arg_token,"%s",string_arguments); and not like how I have it below, this way the string is copied, rather than interpreted.
or
strcpy(arg_token,string_arguments);
In my case, i did not need the double percent sign.
And thanks to jr, got the reply as I was typing this up.