String input in C language sscanf()

function prototype

int sscanf( const char *, const char *, ...);
int sscanf(const char *buffer,const char *format,[argument ]...);
data stored in buffer
format format control string
argument optional set string
sscanf will read the data from the buffer and write the data into the argument according to the format of the format. Include the return value

in the header file <stdio.h> :

Returns the number of parameters on success, -1 on failure, and the cause of the error is stored in errno.
After many tests [source request], what is successfully returned in the linux system is the number of successfully converted values, for example:
sscanf("1 2 3","%d %d %d",buf1, buf2, buf3); The return value of a successful call is 3, that is, buf1, buf2, and buf3 are all successfully converted.
sscanf("1 2","%d %d %d",buf1, buf2, buf3); The return value of a successful call is 2, that is, only buf1 and buf2 are successfully converted.
(Note: all buf here are addresses) 

Description :
Similar to scanf, sscanf is used for input, except that the latter uses the keyboard (stdin) as the input source, and the former uses a fixed string as the input source.
The second argument can be one or more {%[*] [width] [{h | I | I64 | L}]type | ' ' | '\t' | '\n' | not % sign}
Note:
1. * can also be used in the format, (ie %*d and %*s) with an asterisk (*) to indicate that this data is skipped and not read in. (That is, this data is not read into the parameter)
2. {a|b|c} means choose one of a, b, and c, and [d] means that there can be d or no d.
3. width indicates the reading width.
4. {h | l | I64 | L}: The size of the parameter, usually h represents a single-byte size, I represents a 2-byte size, L represents a 4-byte size (except for double), and l64 represents an 8-byte size.
5. Type: This is a lot, that is, %s, %d and the like.
6. Special: %*[width] [{h | l | I64 | L}]type means that those satisfying this condition are filtered out, and no value will be written to the target parameter
Returns 0 on failure, otherwise returns the formatted number of parameters 
set operation:
%[az] means match any character from a to z, greedy (match as many as possible)
%[aB'] matches one of a, B, ', greedy
%[^a] matches any character other than a, and stops reading, the usage of greedy C language function sscanf()

example

1. Common usage.
1  char buf[ 512 ];
 2 sscanf( " 123456 " , " %s " ,buf); // Here buf is the array name, which means to store 123456 in buf in the form of %s! 
3 printf( " %s\n " ,buf);
The result is: 123456
 
2. Take a string of specified length. As in the example below, take a string with a maximum length of 4 bytes.
1 sscanf("123456","%4s",buf);
2 printf("%s\n",buf);
The result is: 1234
 
3. Get the character string up to the specified character. As in the following example, take the string up to any lowercase letter.
1 sscanf("123456abcdedf","%[^a-z]",buf);
2 printf("%s\n",buf);
The result is: 123456
 
4. Take a string containing only the specified character set. As in the example below, take a string containing only 1 to 9 and lowercase letters.
 
1 sscanf("123456abcdedfBCDEF","%[1-9a-z]",buf);
2 printf("%s\n",buf);
The result is: 123456abcdedf
 
When entering: 
1 sscanf("123456abcdedfBCDEF","%[1-9A-Z]",buf);
2 printf("%s\n",buf);

 

The result is: 123456BCDEF (wrong!!!)
 
Note: The result should be: 123456 (because when it encounters a character that is not 1-9 or AZ, that is, when it encounters a lowercase letter, it has ended.)
 
 
 
5. Get the character string up to the specified character set. As in the example below, take the string until it encounters an uppercase letter.
 
1 sscanf("123456abcdedfBCDEF","%[^A-Z]",buf);
2 printf("%s\n",buf);
 
The result is: 123456abcdedf
 
6. Given a string iios/12DDWDFF@122, get the string between / and @,
 
First filter out "iios/", and then send a string of contents other than '@' to buf
sscanf("iios/12DDWDFF@122","%*[^/]/%[^@]",buf);
printf("%s\n",buf);

The result is: 12DDWDFF

 

7. Given a string "hello, world", keep only world.
 
(Note: there is a space after ",", %s stops when a space is encountered, and * is to ignore the first read string)
 
1 sscanf(“hello,world”,"%*s%s",buf);
2 printf("%s\n",buf);
 
The result is: world
 
%*s means that the first matched %s is filtered out, that is, "hello," is filtered out.
 
The result is NULL if there are no spaces.
 
 
8. The most concise format is a tab-spaced string
1 sscanf("string1\tstring2\tstring3", " %s%s%s " ,str1,str2,str3);
 2 printf( " %s\t%s\t%s\ n " ,str1,str2,str3);

The result is: String 1 String 2 String 3


The content is transferred from: https://blog.csdn.net/vitalia/article/details/52474761

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324822077&siteId=291194637