Powerful sscanf Functions

Source: Internet
Author: User

Function prototype:
 
Int sscanf (const char * src, const char * format ,...);
Int scanf (const char * format ,...);
These two functions are very similar, except that the first function uses src as the input, and the second function reads the input using STDIN as the standard input;
Format is a format control string that contains control characters (such as: % d, % I, % s, etc.), blank characters (such: space, Tab \ t, carriage return linefeed \ n or its continuous combination) and non-blank characters;
... Is a set of pointer variables used by the above functions to save the result value;
The return value is the number of pointer variables successfully assigned. If an error occurs in this function, EOF (-1) is returned ).
 
Format control format:
 
Format can be one or more {% [*] [width] [{h | l | I64 | L}] type | white space character | non-white space character}
The symbols in this formula are described as follows:
{A | B | c}: indicates one or more of a, B, and c;
[D]: either d or d;
*: It 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). The usage is as follows:
 
View plainprint?
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

Width: width, which can be ignored. Its usage is as follows:
View plainprint?
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: control characters such as: d, I, f, s, p, and set []. (Note that the % number is at the beginning, you cannot add another "%" here .) Note that the control operator is set % []:

View plainprint?
% [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
 
For example, if the string encounters a space:
View plainprint?
Sscanf ("123456 abcdedf", "% [^]", buf );
Printf ("% s \ n", buf );
Result: 123456
Another example is:
View plainprint?
Const char * s = "iios/12DDWDFF @ 122 ";
Char buf [20];
Sscanf (s, "% * [^/]/% [^ @]", buf );
Printf ("% s \ n", buf );
Result: 12 DDWDFF.
First, filter out "iios/", and then a string of 12 DDWDFF up to the character <a href = "mailto: '@'"> '@' </a>.

 
Blank characters: '', '\ n',' \ t', etc;
Non-blank characters: general characters.
 
Format matching process:
 
This function reads one or more characters from the source (src or STDIN) in the first order and compares them with the characters or strings that are taken from the format control string:
When a blank character is encountered, it is skipped without comparison (What about spaces ?);
When a non-blank character is encountered, it is compared but not saved in the pointer variable;
If a control character can be matched, the characters or strings read from the source are saved to the address indicated by the next pointer according to the meaning of the control character;
Cannot match?
Note:
If the control character contains width, a maximum of so many strings are stored in the address pointed to by the next pointer;
If it is the control character % [], all characters starting from the current position of the source string to the First Matching character that does not conform to the control character are stored in the address pointed to by the next pointer.
 
Common examples:
 
1. Common usage.
Char buf [512];
Sscanf ("123456", "% s", buf); // here buf is the array name, which means to store 123456 in the form of % s into 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
When input:
Sscanf ("123456 abcdedfBCDEF", "% [1-9A-Z]", buf );
Printf ("% s \ n", buf );
Result: 123456
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.
8. Process-am ('-' has spaces on both sides) and Am-Am ('-' has no spaces on both sides ):
The former:
Char sztime1 [16] = "", sztime2 [16] = "";
Sscanf ("2006:0:18-2006:04:18", "% s-% s", sztime1, sztime2 );

The latter:
Char sztime1 [16] = "", sztime2 [16] = "";
Sscanf ("2006:0:18-2006:04:18", "% [0-9,:]-% [0-9,:]", sztime1, sztime2 );

Excerpt from: blog

Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.