National Instruments 320682C Musical Toy Instrument User Manual


 
Formatting and I/O Library Chapter 2
LabWindows/CVI Standard Libraries 2-64 © National Instruments Corporation
String to Real, After Finding a Semicolon in the String
char *s;
double x;
int n;
s = "TIME 12:45:00; 7.34";
n = Scan (s, "%s>%s[xdt59]%f", &x);
/* result: x = 7.34, n = 2 */
Remarks
Some strings returned by programmable instruments contain headers that consist of numeric as
well as non-numeric data and are terminated by a particular character, such as a semicolon. This
example shows how such a header can be skipped.
The format string contains two target specifiers. In the first specifier (
%s[xdt#]
), the
t#
modifier instructs
Scan
to read bytes from
s
until a number is encountered. The
d
modifier
indicates that the bytes must be discarded because there is no argument corresponding to the
specifier. The
x
modifier indicates that the semicolon should also be discarded.
When the
Scan
call succeeds, it returns 2, indicating that two target specifiers were satisfied,
even though there is only one target argument.
String to Real, After Finding a Substring in the String
char *s;
double x;
int index, n;
s = "HEADER: R5 D6; DATA 3.71E+2";
index = FindPattern (s, 0, -1, "DATA", 0, 0) + 4;
n = Scan (s, "%s[i*]>%f", index, &x);
/* result: x = 371.0, n = 1 */
Remarks
This example is similar to the previous one, except that portion of the string to be skipped is
terminated by a substring (
DATA
) rather than by a single character. The Formatting and I/O
Library function
FindPattern
is used to find the index where
DATA
begins in
s
. Four is
added to the index so that it points to the first byte after
DATA
. The index is then passed to
Scan
and matched with the asterisk (
*
) in the format string.
In this example,
FindPattern
returns 15, and
index
is 19. When
index
is matched to the
asterisk in the format string in the
Scan
call, the format string is interpreted as
%s[i19]>%f
.
The
i19
indicates that the first 19 bytes of
s
should be ignored.
Scan
then extracts the real
number from the remaining string,
3.71E+2
, and assigns it to
x
.
Scan
returns 1, indicating
that one target specifier is satisfied.