/* Non-blog summary */
Sscanf
Name:
Sscanf ()-read data that matches the specified format from a string.
Function prototype:
Int sscanf (string str, string fmt, mixed var1, mixed var2 ...);
Int scanf (const char * format [, argument]...);
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}
Note:
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
Example:
[]
1. Common usage.
Char buf [512] =;
Sscanf ("123456", "% s", buf );
Printf ("% s \ n", buf );
Result: 123456
2. Take a string of the specified length. In the following example, a string with a maximum length of 4 bytes is obtained.
Sscanf ("123456", "% 4 s", Buf );
Printf ("% s \ n", Buf );
Result: 1234
3. Obtain the string of the specified character. For example, in the following example, the string is obtained when a space is encountered.
Sscanf ("123456 abcdedf", "% [^]", Buf );
Printf ("% s \ n", Buf );
Result: 123456
4. Take a string that only contains the specified character set. For example, in the following example, take a string that only contains 1 to 9 letters and lowercase letters.
Sscanf ("123456 abcdedfbcdef", "% [1-9a-z]", Buf );
Printf ("% s \ n", Buf );
Result: 123456 abcdedf
5. Obtain the string of the specified character set. For example, in the following example, a string with uppercase letters is used.
Sscanf ("123456 abcdedfbcdef", "% [^ A-Z]", Buf );
Printf ("% s \ n", Buf );
Result: 123456 abcdedf
6. Given a string iios/12DDWDFF @ 122, get the string between/and @ and filter out "iios/" first, then, send a string of content other than '@' to the buf.
Sscanf ("iios/12DDWDFF @ 122", "% * [^/]/% [^ @]", buf );
Printf ("% s \ n", buf );
Result: 12 DDWDFF.
7. Given a string "hello, world", only world is retained. (Note: There is a space after)
Sscanf ("hello, world", "% * s % s", buf );
Printf ("% s \ n", buf );
Result: world
% * S indicates that the first matching % s is filtered out, that is, hello is filtered out.
If there is no space, the result is NULL.
Sscanf is similar to a regular expression, but does not have a strong regular expression. Therefore, we recommend that you use a regular expression for complex string processing.
//-------------------------------------------------------
Sscanf, indicating formatting input from string
In str, input a number to x, which is 32700.
A long time ago, I thought c didn't have its own split string function. Later I found sscanf. For a long time, I thought sscanf could only define strings with spaces. Now I found that I was wrong.
Sscanf is a runtime function. Its prototype is simple:
Int sscanf (
Const char * buffer,
Const char * format [,
Argument]...
);
Its powerful functions are reflected in its support for format.
I used to separate a string like this 2006: 03: 18:
Int a, B, c;
Sscanf ("200:0:18", "% d: % d", a, B, c );
And-2006: 04: 18:
Char sztime1 [16] = "", sztime2 [16] = "";
Sscanf ("2006:0:18-2006:04:18", "% s-% s", sztime1, sztime2 );
But later, I needed to handle
The space on both sides of '-' is canceled, but the % s definition of the string is broken.
I need to re-design a function to handle this situation? This is not complicated, but in order to make all the Code have a uniform style, I need to change many places and replace the existing sscanf with my own split function. I thought I must do this and fell asleep with a strong dissatisfaction with sscanf. I woke up and found that I didn't have.
The format-type has a type field such as %. If the string to be read is not separated by spaces, you can use % [].
% [] Is similar to a regular expression. [A-z] indicates that all characters of a-z are read, and [^ a-z] indicates that all characters except a-z are read.
That's why the problem was solved:
Sscanf ("2006:0:18-2006:04:18", "% [0-9,:]-% [0-9,:]", sztime1, sztime2 );
In softmse (Jake) question post http://community.csdn.net/Expert/topic/4843/4843294.xml? In temp =. 4321558, Zhou Xingxing gave a cool sscanf use case, and then learned how to find that sscanf is awesome.
Original problem:
Iios/12DDWDFF @ 122
How to obtain the string between/and @?
Is there any function in the C program?
Zhou xing's code:
# I nclude <stdio. h>
Int main ()
{
Const char * s = "iios/12DDWDFF @ 122 ";
Char buf [20];
Sscanf (s, "% * [^/]/% [^ @]", buf );
Printf ("% s \ n", Buf );
Return 0;
}
Result: 12 ddwdff.
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.
Function prototype:
Int scanf (const char * Format [, argument]...);
The format can be one or more {% [*] [width] [{H | L | i64 | L}] type | ''| '\ T' | '\ n' | non-% sign },
Note: {A | B | c} indicates A, B, and C. Select [d], which indicates D or D.
Width: width, which can be ignored. Its usage is as follows:
Const char sourcestr [] = "Hello, world ";
Char Buf [10] =;
Sscanf (sourcestr, "% 5 s", Buf); // % 5 s, only 5 Characters
Cout <Buf <Endl;
Result: Hello
{H | l | I64 | L}: parameter size. Generally, h indicates the size of a single byte, I indicates the size of 2 bytes, and L indicates the size of 4 bytes (double exception ), l64 indicates 8-byte size.
Type: this is a lot, that is, % s, % d and so on.
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. For example:
Const char sourceStr [] = "hello, world ";
Char buf [10] =;
Sscanf (sourceStr, "% * s % s", buf); // % * s indicates that the first matching % s is filtered out, that is, hello is filtered out.
Cout <buf <endl;
Result: world
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
Are you familiar with it? Yes, it's very similar to regular expressions, and it still supports filtering, that is, % * [a-z]. For example:
A review of the example of star brother:
Const char * s = "iios/12DDWDFF @ 122 ";
Char buf [20];
Sscanf (s, "% * [^/]/% [^ @]", buf );
Printf ("% s \ n", buf );
First, filter out "iios/", and then send a string of content other than '@' to the buf. The result is obtained.