National Instruments 320682C Musical Toy Instrument User Manual


 
Formatting and I/O Library Chapter 2
LabWindows/CVI Standard Libraries 2-68 © National Instruments Corporation
The first call to Scan assumes that the real number is at the beginning of s. The second call
assumes that the real number starts at the sixth byte of s. The i5 modifier causes the first
5 bytes of s to be ignored.
ASCII File to Two Integers with Error Checking
int file_handle, n, a, b;
file_handle = OpenFile ("FILE.DAT", 1, 2, 1);
if (file_handle < 0) {
FmtOut ("Error opening file\n");
exit (1);
}
n = ScanFile (file_handle, "%s>%i%i", &a, &b);
if (n != 2) {
FmtOut ("Error reading file\n");
exit (1);
}
CloseFile (file_handle);
Remarks
OpenFile opens the file FILE.DAT as an ASCII file for reading only. If OpenFile
succeeds in opening the file, it returns a file handle with a positive integer value. ScanFile
reads the ASCII representation of two integer values from the file. If ScanFile succeeds, it
returns 2 (indicating that two target specifiers were satisfied).
ASCII File with Comma Separated Numbers to Real Array, with Number of Elements at
Beginning of File
double values[1000];
int file_handle, count;
file_handle = OpenFile ("FILE.DAT", 1, 2, 1);
ScanFile (file_handle, "%s>%i", &count);
if (count > 1000) {
FmtOut ("Count too large\n");
exit(1);
}
ScanFile (file_handle, "%s>%*f[x]", count, values);
CloseFile (file_handle);
Remarks
The first ScanFile call reads the number of elements into the integer variable count. If the
value in count exceeds the number of elements in the real array values, an error is reported.
Otherwise, the second ScanFile call matches count to the asterisk (*) in the format string. It