11-29-2010 07:29 AM
Hi All,
I am wondering what would be a good way to scan a string with a variable number of elements to search for. I have seen Roberto's comments on the scan function which are very helpful (link1, link2).
The sort of string I would be scanning would be "5 2.6 4.7 5.01 7.1 4.03" with the 5 specifying that 5 doubles will be following, space-delimited.
Thanks in advance!
Solved! Go to Solution.
11-29-2010 08:11 AM
Here is what I'd do:
Scan (msg, "%d[x]", &numEl);
x = NumFmtdBytes ();
Scan (msg + x, "%*f[x]", numEl, val);
or alternatively:
Scan (msg, "%d[x]", &numEl);
Scan (msg, "%d[dx]%*f[x]", numEl, val);
See the online help, "Scan, ScanFile, ScanIn—Using Asterisks (*) Instead of Constants in Format Specifiers" in Scanning Functions topic.
11-29-2010 09:10 AM
11-29-2010 02:51 PM
CVI's Scan function is very powerful, with many options and capabilities, but it does have some limitations.
For example, the ability to scan multiple parameters into an array as Roberto shows is very slick, but I can't find a way for it to tell me how many items were scanned. Scan returns the number of items formatted, but apparently it counts an array as one item. You can verify that by checking the return value from Scan in Roberto's post.
I tend to fall back on more of the ANSI-C library functions like the strtok() function that Norbert suggested. You need more lines of code, because you're loading the array one element at a time in a loop, but you can add more error checking along the way. You could do the same thing with Scan, reading one parameter at a time, and then skipping the NumFmtdBytes() in the next read. But if I don't need the extra capability of a CVI library function, I use the ANSI-C function instead.
That's just another way of looking at it. strtok() isn't clearly better than Scan() in this case, it just shows another approach. Each has its own pros and cons.
Take a look at the simple example attached. It shows some of the possible error checking that can be done, with examples of several types of possible errors.
02-07-2011 01:41 PM
Thanks Al, that was a very thorough way of doing that!