In my learning process, reading data from a file is a very troublesome thing, fortunately there is a sscanf () function.
The use of C-language function sscanf ()
SSCANF ()-Reads data in a string that matches the specified format.
Function Prototypes:
int string string fmt, mixed var1, mixed var2 ...); intconstchar *format [, argument] ...);
Description
SSCANF is similar to scanf for input, except that the latter takes the screen (stdin) as the input source, the former as the input source with a fixed string.
where format can be one or more {%[*] [width] [{h | l | I64 | L}]type | ' | ' \ t ' | ' \ n ' | Non-% symbol}
Note:
1, * can also be used in the format, (that is,%*d and%*s) with an asterisk (*) indicates skipping this data is not read in. (that is, do not read this data into the parameters)
2, {a|b|c} indicates that A,b,c is selected, [d], indicating that either D or D is not available.
3 and Width indicates the read width.
4, {h | l | I64 | L}: The size of the parameter, usually h represents a single-byte size,i that represents a 4-byte size (double exception) for a 2-byte size,l, and L64 represents a 8-byte size.
5, type: That's a lot, that's%s,%d.
6, Special:%*[width] [{h | l | I64 | L}]type means that the condition is filtered out and the value is not written to the target parameter
supports collection operations:
%[a-z] to match any character from A to Z, greed (as many matches as possible)
%[ab '] matches a, B, ' one member, Greed
%[^a] Match any character that is not a, greedy
Note: when the string being read is an empty string, the SSCANF function does not change the value of the string to be read into .
Example:
1. Common usage.
Char buf[] = ; SSCANF ("" ""%s", BUF); printf ("%s\n", buf);
The result is: 123456
2. Take a string of the specified length. As in the following example, take a string with a maximum length of 4 bytes.
SSCANF ("" "%4s", buf); printf ("%s\n", buf);
The result is: 1234
3. The string to take to the specified character. As in the following example, the string is encountered until the space is met.
SSCANF ("123456 abcdedf""%[^]", BUF); printf ("%s\n", buf);
The result is: 123456
4. Take a string that contains only the specified character set. As in the following example, take a string that contains only 1 to 9 and lowercase letters.
SSCANF ("123456abcdedfBCDEF""%[1-9a-z]", BUF); printf ("%s\n", buf);
The result is: 123456ABCDEDF
5. The string to be taken to the specified character set. As in the following example, take a string that encounters an uppercase letter.
SSCANF ("123456abcdedfBCDEF""%[^a-z]", BUF); printf ("%s\n", buf);
The result is: 123456ABCDEDF
6, given a string iios/[email protected], gets/And the string between @, first filter out "iios/", and then send a string of non-' @ ' to BUF
SSCANF ("iios/[email protected]" "%*[^/]/%[^@]", BUF); printf ("%s\n", buf);
The result is: 12DDWDFF
7, given a string "" Hello, World ", only keep the world. (Note: "," followed by a space)
" %*s%s " , BUF); printf ("%s\n", buf);
The result: World
%*s indicates that the first match to the%s is filtered out, that is, Hello is filtered
If there are no spaces, the result is null.
The SSCANF function is very similar to regular expressions, but there is no strong regular expression, so it is recommended to use regular expressions for more complex string handling.
//-------------------------------------------------------
SSCANF, which represents the formatting of input from a string
The above means that from STR, the input number to X is 32700.
A long time ago, I thought C had no own split string function, and then I found sscanf; all along, I thought sscanf could only delimit strings with spaces, and now I find I'm wrong.
SSCANF is a run-time function, and the prototype is simple:
int sscanf ( constChar *buffer, constchar *format [, argument]. .. );
Its powerful capabilities are reflected in the support for format.
I used to use it to separate strings like this 2006:03:18:
int A, B, C; SSCANF ("2006:03:18""%d:%d:%d", A, B, c);
and 2006:03:18-2006:04:18:
Char sztime1["", sztime2[""; SSCANF ("2006:03:18-2006:04:18""%s-%s", Sztime1, SZTIME2);
But then, I need to deal with 2006:03:18-2006:04:18.
Just canceled the '-' on both sides of the space, but broke the%s of the definition of the string.
Do I need to redesign a function to handle such a situation? It's not complicated, but, in order to have a uniform style for all of the code, I need to change a lot of places to replace the existing sscanf with my own split function. I thought I must have done it, and slept with a strong dissatisfaction with sscanf, waking up and finding it unnecessary.
a Type field such as%[] in Format-type. If you read a string that is not delimited by a space, you can use%[].
%[] is similar to a regular expression. [A-z] means reading all characters A-Z, [^a-z] means reading all characters except A-Z.
So the problem is solved:
SSCANF ("2006:03:18-2006:04:18""%[0-9,:]-%[0-9,:]"
The use of C-language function sscanf ()