Sscanf advanced features

Source: Internet
Author: User

The formatting and output APIs, printf, sprintf, and vsprintf have long been known and the rules for use are also well known:

For example, % d, % s, % P, %-02d, etc;

 

Sscanf is rarely used. In addition to common % d, % s, and % C, sscanf also has "^", "[", "]", and;

1. "^" is the same as the ^ in the regular expression, which is equivalent to non;

2. "[", "]" is used in pairs and serves the same purpose as [,] in a regular expression. It is equivalent to the value range;

3. "*" indicates ignore, which is different from * in the regular expression to take n similar values;

 

Function prototype:

Int sscanf_s (
Const char * buffer,
Const char * Format [,
Argument]...
);

 

The return value indicates the number of matched fields.

 

Take a look at the instance:

 

Const char * src = "1234_abc _"; char szdst [32]; int idst; sscanf (SRC, "% d", & idst); // <! 1) sscanf (SRC, "% C", szdst); // <! 2) sscanf (SRC, "% s", szdst); // <! 3) sscanf (SRC, "% 2 s", szdst); // <! 4) sscanf (SRC, "% [^ _]", szdst); // <! 5) sscanf (SRC, "% * [0-9 _] % [^ _]", szdst); // <! 6) sscanf (SRC, "% d % s", & idst, szdst); // <! 7) sscanf (SRC, "% d _ % s", & idst, szdst); // <! 8)

 

Example 1: format the integer and stop matching when the ASCII code is not in the numeric range. idst = 1234;

Example 2: format a byte. Only one byte is formatted. szdst = 1. The last 31 bytes are the default values when the array is initialized;

Example 3: format the string, with szdst = 1234_abc _ ending with '\ 0;

Example 4: format a fixed-length string, szdst = 12, because the maximum length is 2, % 2 S;

Example 5: Format String Match. When "_" is encountered, the match is stopped. szdst = 1234.

Example 6: format the string and ignore the match. Ignore the characters 0 to 9 and _ and stop matching when _ is encountered. szdst = ABC

 

The above examples all match only one item at the same time. What if we want to match multiple items? Simple addition

In the seventh example, the integer and string are formatted simultaneously. idst = 1234, szdst = _ ABC _

In the eighth example, both formatting and shaping are performed, and the _ in the middle is jumped. idst = 1234, szdst = ABC.

 

In addition to this function, there are also corresponding variants sscanf_l, W, and so on.

 

 

 

 

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.