Function
Sscanf ()-read data that matches the specified format from a string.
Definition
Int sscanf (string STR, string FMT, mixed var1, mixed var2 ...);
Note:
Similar to scanf, sscanf is used for input, but the latter uses the screen (stdin) as the input source, and the former uses a fixed string as the input source.
The format can be one or more {% [*] [width] [{H | L | i64 | L}] type | ''| '/T' | '/ n' | non-% sign}
1. * can also be used in the format. (% * D and % * s) with an asterisk (*) indicates skipping this data and not reading it. (that is, do not read this data into the parameter)
2. {A | B | c} indicates A, B, and C. Select [d], which indicates D or D.
3. width indicates the read width.
4. {H | L | i64 | L}: parameter size. Generally, h indicates a single-byte size, I indicates a 2-byte size, and l indicates a 4-byte size (double exception ), l64 indicates 8-byte size.
5. Type: this is a lot, such as % s and % d.
6. Special: % * [width] [{H | L | i64 | L}] type indicates that values that meet this condition are filtered out and no value is written to the target parameter.
Collection operations are supported:
% [A-Z] indicates matching any character in A to Z, greedy (as many as possible)
% [AB '] matches a, B, and', greedy
% [^ A] matches any character other than a, greedy
A comprehensive example
# Include <string>
# Include <stdio. h>
Int main (INT argc, char * argv [])
{
Char Buf [128];
// Common usage: remove the first two characters
Sscanf ("123456", "% 2 s", Buf );
Printf ("Buf: % s/n", Buf );
// Obtain the string between/and @
Sscanf ("iios/12ddwdff @ 122", "% * [^/]/% [^ @]", Buf );
Printf ("Buf: % s/n", Buf );
// Take a string that only contains the specified character set, and a string that only contains 1 to 9 and lowercase letters.
Sscanf ("123456 abcdedfbcdef", "% [1-9a-z]", Buf );
Printf ("Buf: % s/n", Buf );
// Obtain the string of the specified character and the string of the space
Sscanf ("123456 abcdedf", "% [^]", Buf );
Printf ("str: % s/n", Buf );
// Obtain the string of the specified character set and the string of the uppercase letter.
Sscanf ("123456 abcdedfbcdef", "% [^ A-Z]", Buf );
Printf ("Buf: % s/n", Buf );
// Take the second word. % * s indicates that the first word is ignored and the second word is used.
Sscanf ("Hi Hello", "% * S % s", Buf );
Printf ("Buf: % s/n", Buf );
Int A = 0, B = 0, c = 0;
Sscanf ("200:0:18", "% d: % d", & A, & B, & C );
Printf ("A = % d, B = % d, c = % d/N", A, B, C );
// Separate the usernames and passwords to obtain the usernames and passwords.
/*
1.% * [] Remove the leading space
2.% 127 [^:] takes the first 127 characters. The following error occurs: End
3. ':' delimiter to separate strings
4.% 127 [^] takes the first 127 characters and ends with a space
*/
Char user_name [20], password [20];
Sscanf ("Admin: admin123", "% * [] % 127 [^:]: % 127 [^]", user_name, password );
Sscanf (user_name, "% s", user_name); // remove the trailing space
Printf ("user_name: % s, password: % s/n", user_name, password );
Return 0;
}