usage and examples of sscanf ()Here are some sscanf () some of the use of instructions, are from the Forum, blog organized out. For everyone to use.
Through the study and use of personal thinking, in the string format is not very complex, but it is not easy to use this function more appropriate, this scale depends on their own grasp, the string is not very complex, but their own writing a processing function is troublesome, the efficiency is not high, use this function, if the string is very complex, Then use regular expressions.
Not much to say, look at the following introduction and column bar. Name: SSCANF ()-Reads data in a string that matches the specified format.
Function Prototypes:
Int sscanf (String str, String fmt, mixed var1, mixed var2 ...);
int scanf (const char *format [, argument] ...); Description
SSCANF is similar to scanf, which is used for input, except that the latter takes a screen (stdin) as the input source, the former with a fixed string as the input source.
Where format can be one or more {%[*] [width] [{h | l | I64 | L}]type | ' ' | ' \ t ' | ' \ n ' | Non-% symbol} supports collection operations:
%[a-z] to match any character in a to Z, greed (as many matches as possible)
%[ab ' matches a, B, ' one member, greed
%[^a] matches any character of non-A, greedy example:
1. Common usage.
Char buf[512] = {0};
SSCANF ("123456", "%s", buf);
printf ("%s\n", buf);
The result is: 123456 2. Takes a string of the specified length. As in the following example, take a string with a maximum length of 4 bytes.
SSCANF ("123456", "%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/12ddwdff@122, gets/And the string between @, first filter out "iios/", and then send a string of non-' @ ' to BUF
SSCANF ("iios/12ddwdff@122", "%*[^/]/%[^@]", buf);
printf ("%s\n", buf);
The result is: 12DDWDFF
7, given a string "" Hello, World ", only keep the world. (Note: "," followed by a space) sscanf ("Hello, World", "%*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.
8.
Char *s= "1try234delete5"
The
SSCANF (S, "1%[^2]234%[^5"), S1, S2);
The non-conversion character (before or after the conversion character) that appears in the format of scanf, which is 1234 in this example to skip the corresponding character in the input;
The meaning of ' [] ' is the same as in a regular expression, which indicates that a sequence of characters matches the occurrence of the character; ^ indicates the opposite. The variable that receives input when using [] must be a char, signed char, unsigned char array with sufficient storage space. Remember [is also the conversion character, so there is no s anymore. 8. Split the string marked with a character. Char test[]= "222,333,444,,, 555,666";
Char s1[4],s2[4],s3[4],s4[4],s5[4],s6[4],s7[4];
SSCANF (Test, "%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,]", S1,S2,S3,S4,S5,S6,S7);
printf ("sssa1=%s", S1);
printf ("sssa2=%s", S2);
printf ("sssa3=%s", S3);
printf ("sssa4=%s", S4);
printf ("sssa5=%s", S5);
printf ("sssa6=%s", S6);
printf ("sssa7=%s", S7);
9. An example of extracting an email address from a user's profile
#include <cstdlib>
#include <cstdio>
using namespace Std;
int main ()
{
Char a[20]={0};
Char b[20]={0};
Assume email address information to '; ' End
SSCANF ("email:jimmywhr@gmail.com;", "%*[^:]:%[^;]", a);
Assuming that the email address information does not have a specific end flag
SSCANF ("email:jimmywhr@gmail.com", "%*[^:]:%s", b);
printf ("%s\n", a);
printf ("%s\n", b);
System ("pause");
return 0;
}
The key is "%*[^:]:%[^;]" and "%*[^:]:%s" the two parameters of the problem
%*[^:] means that the conditions in the "[]" will be filtered out and no value will be written to the target parameter. This means that in the
The characters before the first ': ' Are filtered out at the time of writing, ' ^ ' is the meaning of negation, the entire parameter is translated
Into the vernacular is: will be encountered before the first ': ' Before the character (not for ': ') all filter out.
: Nature is the meaning of skipping ': '.
%[^;] Copy characters until you encounter '; '. Excerpt from: http://blog.csdn.net/lbird/archive/2007/08/03/1724429.aspx
%[] Usage:%[] indicates that you want to read in a character set, and if [the first character that follows is "^", it means the opposite.) The string within [] can be 1 or more characters. An empty character set (%[]) is a violation of the rules and can lead to unpredictable results. %[^] is also a violation of the rules.
%[a-z] reads a string between A-Z, if not before stopping, such as Char s[]= "Hello, my Friend"; Note: The comma is not a-Z between sscanf (S, "%[a-z]", string); String=hello
%[^a-z] reads a string that is not a-Z and stops if a character between A-Z is encountered, such as Char s[]= "Hellokitty"; Note: The comma is not a-Z between sscanf (S, "%[^a-z]", string); String=hello
%*[^=] preceded by an * number indicates that no variables are saved. Skips a string that matches a condition. Char s[]= "notepad=1.0.0.1001"; Char szFileName [32] = ""; int i = sscanf (S, "%*[^=]", szFileName); Szfilename=null, because the int i = SSCANF (S, "%*[^=]=%s", szFileName) is not saved; szfilename=1.0.0.1001%40c read 40 characters The Run-time library does not automatically append a null terminator to The string, nor does reading characters automatically terminate the scanf () function. Because The library uses buffered input, you must press the ENTER key to terminate the string scan. If you press the ENTER before the scanf () reads-characters, it's displayed normally, and the library continues to prom PT for additional input until it reads characters
%[^=] reads the string until it touches the ' = ' sign, and ' ^ ' can carry more characters after it, such as: Char s[]= "notepad=1.0.0.1001"; Char szfilename [32] = ""; int i = sscanf (S, "%[^=]", szFileName); szfilename=notepad If the parameter format is:%[^=:], then you can also notepad:1.0.0.1001 Read Notepad use 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 a lot of functions, but not very commonly used, mainly because: 1, many of the system's scanf functions have loopholes. (Typically, TC sometimes goes wrong when entering floating-point types). 2, the use of complex, error prone. 3, the compiler will be very difficult to parse, thus affecting the quality of the target code and execution efficiency. Individuals feel that the 3rd most deadly, more complex features tend to perform less efficiently. And some simple string parsing we can self-process.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.