SCANF family In addition to the scanf function, there are fscanf/sscanf, the three corresponding from the standard input/file/string read, corresponding to the variable parameter version vscanf/vsscanf/vfscanf.
This type of function has a return value, the return value is the number of successful read fields when execution succeeds, and EOF is returned on failure. Note that returning 0 is also a success, except that no valid match is read.
When the function reads the input, it matches the specified pattern format, and once the match fails, the function returns immediately and the input character remains in the buffer, that is, only the characters that match the success are taken.
Here are a few examples to illustrate each.
Example 1: Write a program to read the data from the standard input row by line, and output as the line, pay attention to ignore blank lines, to ensure that the input length of each line is less than 50.
1 Uncle 2 3 This is a test sentence.
Sample Input
1 Uncle 2 This is a test sentence.
Sample Output
1#include <stdio.h>2 intMain ()3 {4 Charbuf[ -];5 intret;6 for (;;) {7ret = scanf ("%[^\n]%*c", buf);8 if(EOF = ret)//no data entered.9 Break;Ten if(0= = ret) {//Blank Line OneFgets (BUF,sizeof(BUF), stdin);//take a carriage return line feed A Continue; - } -printf"%s\n", buf); the } - return 0; -}
View Code
Example 2:mac a Mac that holds a network card in a string, it is now necessary to verify that it is valid, that is, whether it satisfies the "XX:XX:XX:XX:XX:XX" format.
1 intIsvalidmac (Const Char*mac)2 {3 intb[6];4 return 6= = Sscanf (Mac,"%2x:%2x:%2x:%2x:%2x:%2x",5&b[0], &b[1], &b[2], &b[3], &b[4], &b[5]); 6}
View Code
Example 3: The implementation of the Chinese Zodiac gambling machine betting analysis function, with the letter a-l (not case-sensitive), respectively, representing the mouse, cattle ... Dog, Pig This 12 zodiac, bet can choose one or more animals, and can specify the corresponding amount of money, such as not specify the amount, the default is 10 yuan. In order to facilitate the subsequent settlement, the bet data needs to be analyzed and collated. There are multiple lines of input data, one bet per action, and only valid letters, numbers, spaces, and equal signs are included. Each bet is parsed and exported in the original order in the "Letter: Number" format.
1 a=2 acdb=3 b=7 C
Sample Input
1 A: ten B: C:2 A:ten C:ten D:ten B:10 3 B: Ten A: D:7 C:
Sample Output
1#include <ctype.h>2#include <stdio.h>3 #defineValid_lower (n) ((n) >= ' a ' && (n) <= ' L ')4 intMain ()5 {6 Charch1, CH2;7 intVal;8 for (;;) {9 if(EOF = = scanf ("%c", &ch1))//No more dataTen return 0; One if('\ n'= = Ch1) {//Single bet parsing complete Aprintf"\ n"); - Continue; - } the if(! Valid_lower (ToLower (CH1))) {//first word printable character, description is null character, need to skip - ungetc (CH1, stdin); -scanf"%*[\t]%c", &ch1); - } + for(SCANF ("%c", &CH2);' '= = CH2 | |'\ t'= = CH2; scanf"%c", &CH2));//Skip NULL characters - if(Valid_lower (ToLower (CH2)) | |'\ n'= = CH2) {//ch1 with no numbers, take default values + ungetc (CH2, stdin); Aprintf"%c:10", CH1); at}Else if('='==CH2) { -scanf"%d", &val); -printf"%c:%d", Ch1, Val); - } - } - return 0; in}
View Code
scanf class Function description