Formatting and I/O Library Chapter 2
LabWindows/CVI Standard Libraries 2-70 © National Instruments Corporation
Remarks
This example shows how a subroutine can be used to read an array of real numbers from a binary
file. The subroutine takes as parameters a real array, the number of elements to be read, and the
filename.
The ScanFile call reads the first count elements of x from a binary file. The two asterisks
(*) in the format string are matched to count. For instance, if count is 100, then the format
string is equivalent to %100f>100f.
Reading an Integer from the Standard Input
int n, num_readings;
n = 0;
while (n != 1) {
FmtOut ("Enter number of readings: ");
n = ScanIn ("%l>%i", &num_readings);
}
Remarks
This example shows how to get user input from the keyboard. The FmtOut call writes the
prompt string to the screen without a linefeed or carriage return. The ScanIn call attempts to
read an integer value from the keyboard and place it in num_readings. If ScanIn succeeds,
it returns 1, and the loop is exited. Otherwise, the prompt string is repeated.
The format string in the ScanIn call contains a source specifier of %l. This has two
consequences. First, ScanIn returns whenever the user presses ENTER, even if the input line is
empty. This allows the prompt string to be repeated at the beginning of each line until the user
enters an integer value. Second, any characters entered after the integer value are discarded.
Reading a String from the Standard Input
char filename[41];
int n;
n = 0;
while (n != 1) {
FmtOut ("Enter file name: ");
n = ScanIn ("%l>%s[w40q]", filename);
}
Remarks
This example is similar to the previous example, except that the item being read from the
keyboard is a string instead of an integer. The w modifier is used to prevent ScanIn from