% []Usage: % [] indicates to read a character set combination. If [followed by the first character is "^", it indicates the inverse meaning.
The string in [] can be 1 or more characters. The null character set (% []) is in violation of regulations and can
Result in unpredictable results. % [^] Is also in violation of regulations.
% [A-Z]Reads a string between a-Z. If it is not before, it is stopped, as shown in figure
Char s [] = "Hello, my friend"; // note: the comma is between non-a-Z.
Sscanf (S, "% [A-Z]", string); // string = Hello
% [^ A-Z]Reads strings not between A and Z. If a character between A and Z is encountered, it is stopped, as shown in figure
Char s [] = "hellokitty"; // note: the comma is not between a and Z.
Sscanf (S, "% [^ A-Z]", string); // string = Hello
% * [^ =]Front band*No. indicates that no variable is saved. Skips a qualified string.
Char s [] = "Notepad = 1.0.0.1001 ";
Char szfilename [32] = "";
Int I = sscanf (S, "% * [^ =]", szfilename); // szfilename = NULL because it is not saved
Int I = sscanf (S, "% * [^ =] = % s", szfilename); // szfilename = 1.0.0.1001
% 40CRead 40 characters
The run-time
Library does not automatically append a null Terminator
To the string, nor does reading 40 characters
Automatically terminate the scanf () function. Because
Library uses buffered input, you must press the Enter key
To terminate the string scan. If you press the enter before
The scanf () reads 40 characters, it is displayed normally,
And the Library continues to prompt for additional input
Until it reads 40 characters
% [^ =]Read the string until it reaches the '=' sign. '^' can contain more characters, such:
Char s [] = "Notepad = 1.0.0.1001 ";
Char szfilename [32] = "";
Int I = sscanf (S, "% [^ =]", szfilename); // szfilename = notepad
If the parameter format is: % [^ =:], you can also read notepad from notepad: 1.0.0.1001.
Example:
Char s [] = "Notepad = 1.0.0.1001 ";
Char szname [32] = "";
Char szver [32] = "";
Sscanf (S, "% [^ =] = % s", szname, szver); // szname = notepad, szver = 1.0.0.1001
Summary: % [] has many functions, but it is not very common, mainly because:
1. Many system scanf functions have vulnerabilities. (In typical cases, TC sometimes fails to input floating point functions ).
2. Complicated usage and error-prone.
3. It is difficult for the compiler to perform syntax analysis, thus affecting the quality and execution efficiency of the target code.
I personally think that 3rd is the most critical, and the more complicated the functions, the lower the execution efficiency. Some simple string analysis can be processed by ourselves.