LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Scan String with Variable Number Elements?

Solved!
Go to solution

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!

 

0 Kudos
Message 1 of 5
(4,103 Views)

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.



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 2 of 5
(4,091 Views)
Hi TurboMetrologist,

maybe the function strtok could be helpfull:
f.e:

char string[] = "5 2.6 4.7 5.01 7.1 4.03";
char delimiter[] = " ";
char *ptr;
int number_of_elements;
double val[10] = {0};
int element;
first do:

ptr = strtok(string, delimiter);
if (ptr != NULL)
number_of_elements = atoi(ptr);

element = 0;
do {
ptr = strtok(NULL, delimiter);
if (ptr == NULL) break;
val[element++] = atof(ptr);

} while(element != number_of_elements);

It's just an untested example.....

Greeting from Bremerhaven, Germany
Norbert Rieper


"TurboMetrologist" <x@no.email> schrieb im Newsbeitrag
news:1291038014368-1323777@exchange.ni.com...
> Hi All,
> &nbsp;
> 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 (<a title="link1"
> href="http://forums.ni.com/t5/LabWindows-CVI/Can-I-use-the-Scan-format-function-to-break-up-this-string/m-..."
> target="_blank" rel="nofollow">link1</a>, <a title="link2"
> href="http://forums.ni.com/t5/LabWindows-CVI/Max-number-of-elements-in-Scan-function/m-p/712778"
> target="_self" rel="nofollow">link2</a>).
> &nbsp;
> 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.
> &nbsp;
> Thanks in advance!
> &nbsp;


0 Kudos
Message 3 of 5
(4,081 Views)
Solution
Accepted by topic author TurboMetrologist

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.

0 Kudos
Message 4 of 5
(4,058 Views)

Thanks Al, that was a very thorough way of doing that!

 

0 Kudos
Message 5 of 5
(3,776 Views)