National Instruments 320682C Musical Toy Instrument User Manual


 
Chapter 2 Formatting and I/O Library
© National Instruments Corporation 2-65 LabWindows/CVI Standard Libraries
String with Comma-Separated ASCII Numbers to Real Array
char *s;
int n;
double a[5]; /* 5 8-byte real numbers */
s = "12.3, 45, 6.5, -1.3E-2, 4";
n = Scan (s, "%s>%5f[x]", a);
/* result: a[0] = 12.3, a[1] = 45.0, a[2] = 6.5, */
/* a[3] = -0.013, a[4] = 4.0, n = 1 */
Remarks
The
x
modifier causes the comma separators to be discarded.
Scan
considers an array target to be satisfied when at least one element of the array is filled in.
If the source string in this example were
12.3
, only the first element of
a
would be filled in, the
other elements would remain unmodified, and
Scan
would return 1.
Scanning Strings That Are Not NUL-Terminated
int bd;
double x;
char s[20];
ibrd (bd, s, 15);
Scan (s, "%s[w*]>%f", ibcnt, &x);
Remarks
All of the previous examples assume that
s
is a NUL-terminated string. However, when reading
data from programmable instruments using the GPIB and RS-232 Library functions, the data
transferred is not NUL-terminated. This example uses
ibrd
to read up to 15 B from a GPIB
instrument. The global variable
ibcnt
contains the actual number of bytes transferred.
Scan
uses the value in
ibcnt
in conjunction with the
w
modifier to specify the width of the source
string.
For example, if
ibcnt
is 12, the format string is interpreted as
%s[w12]>%f
, causing
Scan
to
use only the first 12 bytes of
s
.
The following example is an alternative method for handling strings that are not
NUL-terminated:
int bd;
double x;
char s[20];
ibrd (bd, s, 15);
s[15] = 0; /* ASCII NUL is 0 */
Scan (s, "%s>%f", &x);