LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

using strings with percent sign

Solved!
Go to solution

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.

 

0 Kudos
Message 1 of 3
(3,605 Views)
Solution
Accepted by topic author ngay528

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

Message 2 of 3
(3,595 Views)

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.

0 Kudos
Message 3 of 3
(3,593 Views)