1 / 28

This presentation includes custom animations.

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.

alessa
Download Presentation

This presentation includes custom animations.

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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.

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

  3. 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 fscanf reads input from a text file. 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 Click Tip sscanf reads input from a string variable. /* VARIABLE DECLARATIONS */ char InputString[81]; Christine S. Wolfe Ohio University Lancaster 2008-Aug-01

  4. syntax diagrams for the scanf cousins from Appendix B of the text • int scanf (const char *format [, address, …]); • int fscanf (FILE *stream, const char *format [, address, …]); • int sscanf (char *buffer, const char *format [, address, …]); All 3 return the number of values read in successfully. All 3 include a control string that specifies what to look for. All 3 include a set of addresses in which to store the input. 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 Christine S. Wolfe Ohio University Lancaster 2008-Aug-01

  5. The syntax of the cousins varies only in the required arguments. • int scanf (const char *format [, address, …]); • int fscanf (FILE *stream, const char *format [, address, …]); • int sscanf (char *buffer, const char *format [, address, …]); scanf has 1 required argument. That required 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 2nd argument is the control string. sscanf has 2 required arguments. The first argument in sscanf identifies the destination string variable and the 2nd argument is the control string. Click Tip 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); Christine S. Wolfe Ohio University Lancaster 2008-Aug-01

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

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

  8. Hitting a key on the keyboard places the value of the keystroke into a buffer in RAM. Keyboard Buffer 1 6 . 5 \t 2 3 \n 2 3 \n Christine S. Wolfe Ohio University Lancaster 2008-Aug-01

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

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

  11. Hitting a key on the keyboard places the value of the keystroke into a buffer in RAM. Keyboard Buffer 1 6 . 5 \t 2 3 \n 7 5 . 2 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. Christine S. Wolfe Ohio University Lancaster 2008-Aug-01

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

  13. Keyboard Buffer 1 6 . 5 \t 2 3 \n 7 5 . 2 1 8 \n 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? scanf getc getchar getch getche Click to see some C functions that take values from the keyboard buffer and store them in a variable. 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 Christine S. Wolfe Ohio University Lancaster 2008-Aug-01

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

  15. the control string… 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

  16. 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. • printf(“The answer is %d”, 3); • printf(“She is %d years old.”, age); • printf(“%d + %d = %d”, numA, numB, numA + numB); 1 placeholder 1 value 1 placeholder 1 value 3 placeholder 3 values Christine S. Wolfe Ohio University Lancaster 2008-Aug-01

  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

  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

  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

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

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

  22. 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. Christine S. Wolfe Ohio University Lancaster 2008-Aug-01

  23. scanf("%f" , &AvgAmount); Click for Step 1: 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. Keyboard Buffer 1 6 . 5 \t 2 3 \n 7 5 . 2 1 8 \n Click for Step 2: The value is stored in the variable, AvgAmount AvgAmount 16.5 Click for Step 3: The stored value is removed from the keyboard buffer and everything moves up. (Notice that the whitespace is still in the buffer.) Keyboard Buffer \t 2 3 \n 7 5 . 2 1 8 \n Christine S. Wolfe Ohio University Lancaster 2008-Aug-01

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

  25. 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. Christine S. Wolfe Ohio University Lancaster 2008-Aug-01

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

  27. Use fflush(stdin); to empty the keyboard buffer. Keyboard Buffer 1 6 . 5 \t 2 3 \n 7 5 . 2 1 8 \n

  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!!!

More Related