LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Scan Function

Hello,
 
Why does the following Scan function not work?  I want to extract individual strings from a single string that holds comma-separated values:
 
 char buff[128];
 char *szStrArray[128];
 
 Fmt(buff, "%s<%s", "1.23,4.56");
 Scan(buff,"%s>%s,%s", szStrArray[0], szStrArray[1]);
 
For example, if buff = "1.23,4.56", then I want szStrArray[0] = "1.23", and szStrArray[1] = "4.56".
 
What I observe is that szStrArray[0] = "1.23,4.56" and szStrArray[1] = "1.23,4.56"
 
I've tried a few variants--the most hopeful one being Scan(buff,"%s>%s[xt44]%s", szStrArray[0], szStrArray[1]);--but this didn't work, either (in fact, that clobbered buff [which it shouldn't have!] and resulted in buff = "4.56" and szStrArray[0] = "4.56" and szStrArray[1] = "4.56").
 
BTW, if I use two individual string variables (i.e. szTemp1 and szTemp2, instead of szStrArray[0] and szStrArray[1]), I still get the same results.
 
Thank you for any input!
0 Kudos
Message 1 of 3
(3,493 Views)
I think I'll just use strtok().
 
It might have been better to implement Fmt and Scan CVI functions so that they conformed to the industry-standard rules and syntax of grep.
 
 
0 Kudos
Message 2 of 3
(3,490 Views)

Seems that the problem is the definition of the destination array. Try using this modified version of your code:

 

int  i;
char buff[128];
char *szStrArray[128];
 
for (i = 0; i < 128; i++) {
 szStrArray[i] = malloc (20);
 memset (szStrArray[i], 0, 20);
}
Fmt(buff, "%s<%s", "1.23,4.56");
Scan(buff,"%s>%s[xt44]%s", szStrArray[0], szStrArray[1]);
DebugPrintf ("Source: %s\nItem1 = %s\nItem2 = %s\n", buff, szStrArray[0], szStrArray[1]);



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 3 of 3
(3,485 Views)