Introduced
SSCANF () is a C language standard library function that reads data that matches the specified format from the specified string. The function prototype is declared in the stdio.h header file:
int sscanf (const char *STR, const char *format, ...);
The function converts the format
string that the parameter str points to, based on the parameter (the formatted string), and the converted result is stored in the corresponding variable parameter. The return value is the number of variable parameters that are read in and assigned in the specified format Fu Chenggong (if a matching error occurs and partially succeeds, the number is less than the specified number of arguments, or even 0). If the input is closed before the first successful conversion or error match occurs (for example, str is an empty string), EOF is returned. EOF is also returned when a read error occurs, and the error code is set errno (for example, format
when a null pointer returns EOF and sets errno to Einval). You can see whether the format conversion was successful by comparing the return value of the function with the specified number of variable parameters.
format
can be one or more {%[*] [width] [{h | l | L}]type | ' ' | '\t' | '\n' | 非%符号}
format converters. The collection {a|b|c}
represents the format character A, b, and C as an optional. The format character, enclosed in brackets, is optional. % and type are required, and all format characters must begin with a%.
The following briefly describes the meaning of each format character:
1) The assignment suppressor ' * ' indicates that the input is read as indicated by the subsequent converter, but discards it without assignment ("skipped"). The suppressor does not require a corresponding pointer-variable parameter, nor does the conversion count the number of successful assignments returned by the function to indicate that the 。%*[width] [{h | l | L}]type
character that satisfies the condition is filtered out and does not assign a value to the target parameter.
2) width
represents the maximum read width. Stops reading when the number of read characters exceeds this value, or when mismatched characters are encountered. Most conversions discard the starting whitespace characters. These discarded characters and the null terminator (' ") added by the conversion result do not count to the maximum read width.
3) {h | l | L}
is a type modifier. h indicates that numeric values entered are short int
stored in or unsigned short int
type, hh
indicating that the input is stored as or as a signed char
unsigned char
type. L (lowercase l) indicates that the long int
input unsigned long int
is stored with, or double
type, if combined with%c or%s, indicates that the input is stored in a wide-character or wide string; ll equals L. L indicates that input is long long
stored as type.
4 type
is a type conversion character, such as%s,%d.
In addition, there are two special format characters:
1) []: Character set combination. [] indicates that the specified character set matches the non-empty sequence of characters; This action does not skip white-space characters (spaces, tabs, or newline characters), so it can be used when the destination string is not delimited by a whitespace character. [] There can be one or more non-^ characters (including hyphens '-') and no order requirements. %[a-z] represents any character that matches A to Z,%[ab-] matches any character in a, B, and-%[^a] matches any character not a, that is, all characters before the first A (not a). ^ can be used for multiple conditions, such as ^a-z= representing ^a-z and ^= (neither lowercase nor equal). Empty character Set%[] and%[^] can cause unpredictable results.
Parameters that receive input when using [] must be a sufficient amount of storage space char、signed char
or unsigned char
an array. [] is also a converter, so%[] has no S.
%[^] has the same meaning and usage as regular expressions, so the sscanf
function provides a simple regular expression function to some extent.
2 N: The number of equivalent characters of the read value (not necessarily assigned), which must be stored as type int. If "10,22" is converted after the "%d%*[^0-9]%n" format, the%n corresponding parameter value is 3 (although ', ' is not participating in the assignment).
' n ' is not a conversion character, although it can be suppressed by ' * '. The C standard asserts that the execution of the%n instruction does not increase the number of assignments returned by the function, but the description in its errata contradicts it. It is not recommended to assume the effect of%n on the return value.
The following table enumerates the common formatting usages of the SSCANF function:
In addition, there are several uses:
"Example 1" read one line of string
%s cannot be used directly because the string may contain whitespace characters, and gets
the function has an overflow risk and is not recommended for use. At this point, you can use the sscanf
function and format the string as "%[^\n]%*c". %*c is used to skip the newline character \ n to read the next line again.
" Example 2" extracts "name" in "name = Yuan"
If there are white characters at the beginning of the line, the "%*[ \t]%[^= \t]"
format string is available;
If you are unsure whether white characters are available at the beginning of the line, you can skip white characters first:
Char szname[] = "Name = Yuan";
Char szresbuf[32] = {0};
SSCANF (SZNAME+STRSPN (szName, "T"), "%[^= \ t]", SZRESBUF);
"Example 3" Decomposition URL
The common implementation looks like this:
/***************************************************************************** * Function Name: OaSplitPwFarEndIpInfo * Function Description: To decompose remote IP information into destination IP address and port number * Note: Remote IP information should be shaped like ' udp://192.168.100.221:5000 ' ****************************************** /static Func_status Oasplitpwfarendipinfo (int8u *pucfarendipinfo, int32u *
Dwdstudpport, int8u *pucdstipaddr) {func_status retCode = S_OK;
int8u strudphead[] = "udp://";
int8u Ucudpurllen = strlen (Strudphead);
int8u ucindex = 0;
Check_triple_pointer (Pucfarendipinfo, Dwdstudpport, pucdstipaddr, S_null_pointer); if (strncasecmp (Pucfarendipinfo, Strudphead, Ucudpurllen)!= 0) {omcilog (Log_ces, "[%s]cannot Parse-farendipinfo (%s)!")
\n\r ", __function__, Pucfarendipinfo);
return s_error; } int8u Ucmaxurllen = Ucudpurllen + Str_ipv4_max_len; Avoid a dead loop (infinite loop) for when the port is not configured (Ucindex = 0; (Pucfarendipinfo[ucudpurllen]!= ': ') && (Ucudpurllen < Ucmaxurllen); ucindex++) {Pucdstipaddr[ucindex] = pucfarendipinfo[ucudpurllen++];
} Pucdstipaddr[ucindex] = ';
*dwdstudpport = Strtoul (&pucfarendipinfo[ucudpurllen+1], NULL, 10);
return retCode; }
using sscanf formatting is simpler:
Char szurl[] = "udp://192.168.100.221:5000";
Char Szprot[4] = {0}, szip[32] = {0};
unsigned int dwport = 0;
SSCANF (Szurl, "%[^://]%*c%*c%*c%[^:]%*c%d", Szprot, Szip, &dwport);
printf ("szprot=%s, szip=%s, dwport=%d\n", Szprot, Szip, Dwport);
"Example 4" extracts the number
Char szdig[]= "10,22m,z86,,880;555:666."
int dwidx = 0, Dwval = 0, dwsize = 0;
while (1 = = sscanf (szdig+dwidx, "%d%*[^0-9]%n", &dwval, &dwsize))
{
dwidx = dwsize;
printf ("dwidx=%d, dwsize=%d, dwval=%d\n", Dwidx, dwsize, dwval);
This implementation is slightly modified and can be used to handle a string of characters separated by a number.
Summarize
In summary, for simple string analysis, the use of sscanf function processing is relatively simple. If the string is more complex, you can use the appropriate regular expression library. Note that the purpose of the SSCANF format is to "intercept" and the regular expression is intended to "match" and not be identical.
The above is the whole content of this article changed, I hope that the study can help, if the question is welcome to leave a message discussion.