Formatting Output

Chapter 11 - Complete I/O in C

Visual C++ 6: The Complete Reference
Chris H. Pappas and William H. Murray, III
  Copyright 1998 The McGraw-Hill Companies

Formatting Input
Formatted input can be obtained for a C program by using the very versatile functions scanf( ) and fscanf( ). The main difference between the two functions is that the latter requires that you specifically designate the input file from which the data is to be obtained.
Using scanf( ), fscanf( ), and sscanf( )
You can use all three input functions, scanf( ), fscanf( ), and sscanf( ), for extremely sophisticated data input. For example, look at the following statement:
scanf(“%2d%5s%4f”,&ivalue,psz,&fvalue);
The statement inputs only a two-digit integer, a five-character string, and a real number that occupies a maximum of four spaces (2.97, 12.5, and so on). See if you can even begin to imagine what this next statement does:
scanf(“%*[ \t\n]\”%[^A-Za-z]%[^\"]\"",ps1,ps2);
The statement begins by reading and not storing any white space. This is accomplished with the following format specification: “%*[ \t\n]”. The * symbol instructs the function to obtain the specified data but not to save it in any variable. As long as only a space, tab, or newline is on the input line, scanf( ) will keep reading until it encounters a double quote (). This is accomplished by the \” format specification, which says the input must match the designated symbol. However, the double quote is not input.
Once scanf( ) has found the double quote, it is instructed to input all characters that are digits into ps1. The %[^A-Za-z] format specification accomplishes this with the caret (^) modifier, which says to input anything not an uppercase letter “A” through “Z” or lowercase letter “a” through “z”. Had the caret been omitted, the string would have contained only alphabetic characters. It is the hyphens between the two symbols “A” and “Z” and “a” and “z” that indicate the entire range is to be considered.
The next format specification, %[^\"], instructs the input function to read all remaining characters up to but not including a double quote into ps2. The last format specification, \", indicates that the string must match and end with a double quote. You can use the same types of input conversion control with the functions fscanf( ) and sscanf( ). The only difference between the two functions scanf( ) and fscanf( ) is that the latter requires that an input file be specified. The function sscanf( ) is identical to scanf( ) except that the data is read from an array rather than a file.
The next example shows how you can use sscanf( ) to convert a string (of digits) to an integer. If ivalue is of type int and psz is an array of type char that holds a string of digits, then the following statement will convert the string psz into type int and store it in the variable ivalue:
sscanf(psz,"%d",&ivalue);
Very often, the functions gets( ) and sscanf( ) are used in combination, since the function gets( ) reads in an entire line of input and the function sscanf( ) goes into a string and interprets it according to the format specifications.
One problem often encountered with scanf( ) occurs when programmers try to use it in conjunction with various other character input functions such as getc( ), getch( ), getchar( ), gets( ), and so on. The typical scenario goes like this: scanf( ) is used to input various data types that would otherwise require conversion from characters to something else. Then the programmer tries to use a character input function such as getch( ) and finds that getch( ) does not work as expected. The problem occurs because scanf( ) sometimes does not read all the data that is waiting to be read, and the waiting data can fool other functions (including scanf( )) into thinking that input has already been entered. To be safe, if you use scanf() in a program, don’t also use other input functions in the same program.
Chapter 12 introduces you to the basics of C++ I/O. Chapters 13 through 16 explain the concepts necessary to do advanced C++ I/O, and Chapter 17 completes the subject of I/O in C++.

Books24x7.com, Inc 2000 –  


Visual C++ 6(c) The Complete Reference
Visual Studio 6: The Complete Reference
ISBN: B00007FYGA
EAN: N/A
Year: 1998
Pages: 207

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net