National Instruments 320682C Musical Toy Instrument User Manual


 
Chapter 2 Formatting and I/O Library
© National Instruments Corporation 2-59 LabWindows/CVI Standard Libraries
Scan/ScanFile/ScanIn Examples in C
This section contains examples of program code that use the Scan, ScanFile, and ScanIn
functions from the Formatting and I/O Library. To eliminate redundancy, the examples include
no error checking on I/O operations in this section except for the ASCII File to Two Integers with
Error Checking example.
String to Integer
char *s;
int a, n;
s = "32";
n = Scan (s, "%s>%i", &a); /* result: a = 32, n = 1 */
s = "-32";
n = Scan (s, "%s>%i", &a); /* result: a = -32, n = 1 */
s = " +32";
n = Scan (s, "%s>%i", &a); /* result: a = 32, n = 1 */
s = "x32";
n = Scan (s, "%s>%i", &a); /* result: a = ??, n = 0 */
Remarks
When locating an integer in a string, Scan skips over white space characters such as spaces,
tabs, linefeeds, and carriage returns. If a non-numeric character other than a white space
character, +, or - is found before the first numeric character, the Scan call fails. Thus, Scan
fails on the x in x32; it leaves the value in a unmodified and returns zero, indicating that no
target specifiers were satisfied.
s = "032";
n = Scan (s, "%s>%i", &a); /* result: a = 32, n = 1 */
s = "32a";
n = Scan (s, "%s>%i", &a); /* result: a = 32, n = 1 */
s = "32";
n = Scan (s, "%s>%o", &a); /* result: a = 26, n = 1 */
s = "32";
n = Scan (s, "%s>%x", &a); /* result: a = 50, n = 1 */
Remarks
When the %i specifier is used, numeric characters are interpreted as decimal, even when they
might appear to be octal (as in 032) or hexadecimal (as in 32a). When the %o specifier is
used, the numeric characters (01234567) are always interpreted as octal. When the %x
specifier is used, the numeric characters (0123456789abcdef) are always interpreted as
hexadecimal.
s = "32x1";
n = Scan (s, "%s>%i", &a); /* result: a = 32, n = 1 */