Defines the function int sscanf (const char *str,const char * format,........);
Function description
SSCANF () Converts the string of the parameter str according to the parameter format string and formats the data. Format conversion form please refer to scanf (). The converted result is stored in the corresponding parameter.
An example:
Main ()
{
int i;
Char s[5];
SSCANF ("1/1", "%[^/]/%d", s,&i);
printf ("%d%s\n", I, s);
}
Results:
1 1
This function extracts a string or number from a string, in a specific format, and remembers it as a string or a number.
%d%f%x, etc. are all arrays
%s%[] All in string
* with * number is filter
The above format parsing
%[^/]/%d
%[^/] is the string s before extracting "/"
/Filter Out/
%d is the extraction of a decimal number to I
The following copy is only the http://blog.csdn.net/gundam_00/article/details/5113501 of another blogger in the blog park
*************************************************************************************************************** ******************************
As we all know, sscanf is a useful function to take out integers, floating-point numbers, and strings from a string. It is simple to use, especially for integers and floating-point numbers. But newbies may not know some of the high-level uses of string handling, so here's a quick explanation.
1. Common usage.
The following is a reference fragment:
CHARSTR[512]={0};
SSCANF ("123456", "%s", str);
printf ("str=%s
", str);
2. Take a string of the specified length. As in the following example, take a string with a maximum length of 4 bytes.
The following is a reference fragment:
SSCANF ("123456", "%4s", str);
printf ("str=%s
", str);
3. The string to take to the specified character. As in the following example, the string is encountered until the space is met.
The following is a reference fragment:
SSCANF ("123456abcdedf", "%[^]", str);
printf ("str=%s
", str);
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.
The following is a reference fragment:
SSCANF ("123456abcdedfBCDEF", "%[1-9a-z]", str);
printf ("str=%s
", str);
5. The string to be taken to the specified character set. As in the following example, take a string that encounters an uppercase letter.
The following is a reference fragment:
SSCANF ("123456abcdedfBCDEF", "%[^a-z]", str);
printf ("str=%s", str);
*************************************************************************************************************** ******************************
Name: SSCANF ()-Reads data in a string that matches the specified format.
Syntax: int sscanf (String str, String fmt, mixed var1, mixed var2 ...);
Integer sscanf (String str, String fmt, mixed var1, mixed var2 ...);
Usage: Use FMT in the specified format to interpret the string str. In addition to%d and%s, the FMT can contain other strings as formats. Each%d or%s corresponds to a parameter, in order var1, var2 ...%d reads an integer into the argument, and%s reads a string. * can also be used in the format, (i.e.%*d and%*s) with an asterisk (*) indicating that skipping this data is not read in. (that is, do not read this data into the parameters) The LPC sscanf () is similar to C's sscanf (), but it is still different. LPC sscanf () does not need (and cannot) provide the memory address of the variable to sscanf (), just give the name of the variable. Another difference is that LPC sscanf () for: sscanf (str, "%s", str1, str2); Syntax, the first English word in Str (that is, the previous content of the first whitespace character) is read into str1, and the rest of the content is read into the str2.
SSCANF () returns the total number of%d and%s in compliance format.
Previously written such a small program: a text file, each row is a record, each record contains more than one field, each field is separated by some delimiter, for example, as follows:
Notebook,ibm,thinkpad x32,6,12000
(The fields are separated by commas and the contents are: item name, manufacturer, model, quantity, price)
If you want to process such a row of records, extract the various fields, how to do better?
My previous practice was to use the Strtok function in a loop to fetch one field at a time, and then save the contents to a string array. Although this is possible, I always feel that the code I write is a bit verbose.
Recently saw a piece of code, using C's standard library function sscanf, processing such data, just one line. I tidied up the code and removed the irrelevant parts, the core part is as follows:
float price;
int quantity;
Char category[21], name[21];
Char vendor[21], sku[21];
Char buf[201];
fp = fopen (filename, "R");
Fgets (buf, (+), FP);
SSCANF (BUF,
"%20[^#]#%20[^#]#%f#%i#%20[^#]#%20[^/n]",
Name, SKU, &price, &quantity, category, vendor);
Here are some simple explanations:
%20[^#]# reads a maximum of 20 characters until it encounters a delimiter #, but does not contain a delimiter
%f# reads a floating-point number until it encounters a delimiter #
%i# reads an integer until it encounters a delimiter #
%20[^/N] reads up to 20 characters, ignoring carriage returns at the end of a line
Is it very concise and clear?
#include <stdio.h>
int main ()
{
Char log[]= "<14>2002-11-11 12:12:12 11.22.33.44 3 3 aaaa aaaaaa";
Char log[]= "<1>2002-11-11 12:12:12 11.22.33.44 3 aaaa aaaaaa";
Char test[]= "<1111> 22";
Char log2[200];
Char str1[20];
Char str2[20];
Char str3[20];
Char str4[20];
Char str5[20];
Char str6[20];
Char str7[20];
int a1,a2,a3,a4,a5,a6;
sscanf (log, "<%d>%s%s%s%d%d%s", &A1,STR2,STR3,STR4,&A5,&A6,STR7);
printf ("%d/n", A1);
printf ("%s/n", str2);
printf ("%s/n", STR3);
printf ("%s/n", STR4);
printf ("%d/n", A5);
printf ("%d/n", A6);
printf ("%s/n", STR7);
sscanf (Test, "<%d>%d", &A5,&A6);
printf ("%d/n", A5);
printf ("%d/n", A6);
sscanf (log, "<%[^>]>%[^"%[^]%[^]%[^]%[^]%[^$] ", STR1,STR2,STR3,STR4,STR5,STR6,STR7);
printf ("%s/n", str1);
printf ("%s/n", str2);
printf ("%s/n", STR3);
printf ("%s/n", STR4);
printf ("%s/n", STR5);
printf ("%s/n", STR6);
printf ("%s/n", STR7);
return 1;
}
const char *STR = "Drw-rw-rw-1 user group 0 Oct 28 2003-==== special ftp=====-for pop music";
Above is the source string, I want to get drw-rw-rw-,group,-==== pop special ftp=====-field separately
Note: Because the values of these fields will change, so I want to use formatted input, respectively, into the following a B C, expert help!
And here's what I tried to do without success.
Char a[20];
Char b[50];
Char c[20];
int ret = sscanf (str, "%[^" "]*%[" U "]%[^" "
, A, B, c);
Masterz (www.fruitfruit.com) at 2005-8-6 17:43:49
SSCANF can support regular expression? Maybe you want to use boost, here's a simple example, though it's a little bit far from your problem.
Http://www.fruitfruit.com/vc/boost/boost_regex_test.cpp
Newbiestar on 2005-8-6 18:29:18
Landlord a question several places ask ...
int ret = sscanf (str, "%s%*s%*s%s%*s%*s%*s%*s%s", A, B, c);
That's all you got, all right?
The use of the SSCANF function