Presentation is loading. Please wait.

Presentation is loading. Please wait.

This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed.

Similar presentations


Presentation on theme: "This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed."— Presentation transcript:

1

2 This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed. If you have opened this lesson in PowerPoint, use the PowerPoint menus to view it in slide show mode. If you have opened this lesson in a browser and see a bar similar to that below, click on the Slide Show icon A notice similar to the one below may appear warning that ActiveX or other scripts are disabled. Enable the controls for this website in order to see the animations.

3 scanf Christine S. Wolfe Ohio University Lancaster 2008-Aug-01 This lesson describes the syntax and use of the scanf library functions: scanf() fscanf() sscanf() Vocabulary: To use scanf, fscanf, or sscanf, you must #include Click Tip ASCII conversion specifier file name extension FILE pointer flag fscanf() placeholder precision scanf() result sscanf() stdin text file unicode

4 3 Christine S. Wolfe Ohio University Lancaster 2008-Aug-01 The scanf functions are general-purpose input routines that read from a stream and store the information in the variables pointed to in the argument list scanf reads input from stdin. stdin is a predefined macro that represents the standard output and is typically the keyboard although this can be changed by the end user. Click Tip Click Tip A text file is one in which every byte is a character from the ASCII, Unicode, or other character set. Some text files have a filename extension other than.txt sscanf reads input from a string variable. fscanf reads input from a text file. /* VARIABLE DECLARATIONS */ char InputString[81];

5 4 Christine S. Wolfe Ohio University Lancaster 2008-Aug-01 syntax diagrams for the scanf cousins from Appendix B of the text All 3 include a set of addresses in which to store the input. All 3 include a control string that specifies what to look for. All 3 return the number of values read in successfully. Although the square brackets indicate that the arguments at the end are optional, that is a bit misleading. They are required if there are any placeholders in the output string. There must be one argument (value) provided for each placeholder. Click Tip int scanf (const char *format [, address, …]); int fscanf (FILE *stream, const char *format [, address, …]); int sscanf (char *buffer, const char *format [, address, …]);

6 5 Christine S. Wolfe Ohio University Lancaster 2008-Aug-01 The syntax of the cousins varies only in the required arguments. sscanf has 2 required arguments. The first argument in sscanf identifies the destination string variable and the 2 nd argument is the control string. fscanf has 2 required arguments. The first argument in fscanf identifies the file pointer for the destination file and the 2 nd argument is the control string. scanf has 1 required argument. That required argument is the control string. stdin is treated like a file pointer in C so the following 2 constructions produce the same result: scanf("%d", &ID); fscanf(stdin, "%d", &ID); Click Tip int scanf (const char *format [, address, …]); int fscanf (FILE *stream, const char *format [, address, …]); int sscanf (char *buffer, const char *format [, address, …]);

7 6 Christine S. Wolfe Ohio University Lancaster 2008-Aug-01 Hitting a key on the keyboard places the value of the keystroke into a buffer in RAM. Keyboard Buffer 1 16.5 \t 6.5

8 7 Christine S. Wolfe Ohio University Lancaster 2008-Aug-01 Hitting a key on the keyboard places the value of the keystroke into a buffer in RAM. Keyboard Buffer 16.5 \t Notice that hitting the tab key places the value '\t' in one byte of the keyboard buffer.

9 8 Christine S. Wolfe Ohio University Lancaster 2008-Aug-01 Hitting a key on the keyboard places the value of the keystroke into a buffer in RAM. Keyboard Buffer 16.5 23 \n \t 23 \n

10 9 Christine S. Wolfe Ohio University Lancaster 2008-Aug-01 Keyboard Buffer 16.52 \t 3 \n Notice that hitting the enter/return key, places the value '\n' in one byte of the keyboard buffer.

11 10 Christine S. Wolfe Ohio University Lancaster 2008-Aug-01 Hitting a key on the keyboard places the value of the keystroke into a buffer in RAM. Keyboard Buffer 7 75.2 5.2 16.52 \t 3 \n

12 11 Christine S. Wolfe Ohio University Lancaster 2008-Aug-01 Hitting a key on the keyboard places the value of the keystroke into a buffer in RAM. Keyboard Buffer 75.2 16.52 \t 3 \n Notice that hitting the spacebar, places the value ' ' in one byte of the keyboard buffer. A space is a value. It is not the same as NULL.

13 12 Christine S. Wolfe Ohio University Lancaster 2008-Aug-01 Hitting a key on the keyboard places the value of the keystroke into a buffer in RAM. Keyboard Buffer 75.21 8 \n 18 16.52 \t 3 \n

14 13 Christine S. Wolfe Ohio University Lancaster 2008-Aug-01 Now we understand how values get into the keyboard buffer. The next question is how do they get out of the keyboard buffer and into a variable? Keyboard Buffer 75.21 8 \n 16.52 \t 3 \n Actually, the operating system uses API calls as intermediaries between the application calls and the keyboard buffer but understanding APIs is beyond the scope of this class and is not necessary for understanding the C I/O functions. Click Tip Click to see some C functions that take values from the keyboard buffer and store them in a variable. scanf getc getchar getch getche

15 14 Christine S. Wolfe Ohio University Lancaster 2008-Aug-01 The control string is made up of 3 types of items: format specifiers white spaces non-white space characters int scanf (const char *format [, address, …]); memory addresses (ie: variables) There are 2 types of arguments in scanf: the control string adapted fromSchildt. C/C++ Programmer’s Reference Following the control string, the arguments must be addresses of variables. & precedes the names of scalar variables No & precedes the names of arrays nor do [ ]’s follow the name. scanf(“%d %s”, &age, name); This miniLesson is about scanf so that is what we will look at now.

16 15 int scanf( control string, arg1, …, argn); Format specifiers Input format specifiers begin with a sign and tell scanf() what type of data is to be read next. The format string is read left to right and the format specifiers are matched, in order, with the arguments that comprise the argument list. adapted fromSchildt. C/C++ Programmer’s Reference specifierReads %ca single char %dany integer (decimal, octal, or hex) %iA decimal integer %fa floating-point number %sa string %[ ]scanset %a percent sign the control string…

17 16 printf(“The answer is %d”, 3); printf(“She is %d years old.”, age); printf(“%d + %d = %d”, numA, numB, numA + numB); Christine S. Wolfe Ohio University Lancaster 2008-Aug-01 For each placeholder, there must be a value in the argument list. The data type of the value must match or be cast as the value indicated by the format specifier. The order in which the placeholders appear in the output string must exactly match the order of the values in the argument list. 1 placeholder 1 value 3 placeholder 3 values

18 17 int scanf( control string, arg1, …, argn); Whitespace characters A whitespace character is either a space a tab character a newline A whitespace character in the format string causes scanf to skip over zero or more whitespace characters in the input stream. In essence, one whitespace character in the control string will cause scanf() to read, but not store, any number of whitespace characters up to the first nonwhitespace character. scanf(“\n%d”, &iAge); Note, including a whitespace at the beginning of the control string will also serve to flush any newlines at the beginning of the buffer. This is very useful. adapted fromSchildt. C/C++ Programmer’s Reference

19 18 int scanf( control string, arg1, …, argn); Nonwhitespace characters A nonwhitespace character in the format string causes scanf to read and discard a matching character. If the specified character is not found, scanf will terminate. scanf(“%d,%d”, &iAge, &iSize); The above will work with an input stream of 10,20 It will fail with 10 20 scanf(“%d %d”, &iAge, &iSize); The reverse is true with the scanf above. It will FAIL on 10,20 It will succeed with 10 20 adapted fromSchildt. C/C++ Programmer’s Reference

20 19 int scanf( control string, arg1, …, argn); scanset A scanset defines a set of characters that will be read by scanf and assigned to the corresponding character array. A scanset is defined by putting the characters you want to scan for inside square braces [ ]/ the beginning square brace must be prefixed by a percent sign. For example, %[ABC] tells scanf to read only the chars A, B, and C When a scanset is used, scanf continues to read characters and put them into the corresponding character array until a character that is not in the scanset is encountered. The corresponding variable must be a pointer to a character array. Upon return from scanf, the array will contain a null-terminated string comprised of the characters read. inverted scanset Including a carat ^ as the first character in a scanset instructs scanf to accept any character NOT included in the scan set. %[^XYZ] adapted fromSchildt. C/C++ Programmer’s Reference

21 20 Christine S. Wolfe Ohio University Lancaster 2008-Aug-01 Keyboard Buffer 75.21 816.52 \t 3 \n 3 characters are considered "whitespace": \t (Tab) \n (newline created by hitting the Enter/Return key) (a blank space) There are 4 whitespaces in the keyboard buffer above. \n the control string…

22 21 Christine S. Wolfe Ohio University Lancaster 2008-Aug-01 Keyboard Buffer 75.216.52 \t 3 \n scanf treats a set of contiguous whitespace characters as 1 single whitespace. There are 2 whitespaces in the keyboard buffer above. \t 1 whitespace

23 22 Christine S. Wolfe Ohio University Lancaster 2008-Aug-01 scanf "scans" the keyboard buffer to find the items specified in the control string. scanf("%f", &AvgAmount); This statement says to read a float from the keyboard buffer and store it in AvgAmount.

24 23 Christine S. Wolfe Ohio University Lancaster 2008-Aug-01 Keyboard Buffer 75.21 8 \n 16.52 \t 3 \n scanf("%f", &AvgAmount); AvgAmount16.5 scanf starts at the beginning of the keyboard buffer and reads until it encounters a whitespace or a character that cannot be part of the float. Click for Step 1: Click for Step 1: The value is stored in the variable, AvgAmount Click for Step 2: Click for Step 2: Keyboard Buffer 75.21 8 \n 2 \t 3 \n The stored value is removed from the keyboard buffer and everything moves up. (Notice that the whitespace is still in the buffer.) Click for Step 3: Click for Step 3:

25 24 Christine S. Wolfe Ohio University Lancaster 2008-Aug-01 The keyboard buffer waits in this state for the next call that accesses the keyboard. Keyboard Buffer 75.21 8 \n 2 \t 3 \n

26 25 Christine S. Wolfe Ohio University Lancaster 2008-Aug-01 scanf(“\n%d %f", &ItemCount, &ItemAmount); This statement says to read a whitespace from the keyboard buffer followed by an integer followed by a whitespace followed by a float and to store the integer in ItemCount and the float in ItemAmount.

27 scanf(“\n%d %f", &ItemCount, &ItemAmount); Click for after Step 1: Click for after Step 1: Click for after Step 2: Click for after Step 2: Click for after Step 3: Click for after Step 3: Click for after Step 4: Click for after Step 4: Keyboard Buffer 75.21 8 \n 23 ItemCount ItemAmount Keyboard Buffer 75.21 8 \n 23 ItemCount ItemAmount Keyboard Buffer 1 8 \n 23 ItemCount 75.2 ItemAmount Keyboard Buffer 75.21 8 \n 2 \t 3 \n ItemCount ItemAmount Keyboard Buffer 75.21 8 \n 23 ItemCount ItemAmount Starting memory

28 Keyboard Buffer 27 75.1 8 \n 16.2 \t 3 \n Use fflush(stdin); to empty the keyboard buffer. 52

29 28 In this class, unless you have a specific reason to do otherwise, ALWAYS begin your scanf statements as follows: fflush(stdin); scanf(“\n The fflush statement will remove any typeahead characters from the keyboard buffer ensuring that the keyboard buffer contents were entered after the user read your prompt. The \n will cause the scanf statement to ignore any whitespace that appears before the content you want to retrieve from the input stream. Why? It bears repeating… ALWAYS begin your scanf statements as follows: fflush(stdin); scanf(“\n Click Tip …if you don’t, don’t say you weren’t warned!!!


Download ppt "This presentation includes custom animations. To view the animations, you must view the presentation in Slide Show mode and activeX controls must be allowed."

Similar presentations


Ads by Google