Advanced sscanf usage

Source: Internet
Author: User
Tags format definition

Printf or sprintf must be the most common function of any C programmer. In contrast to sprintf, sscanf is usually used to parse and convert strings, and its format definition is flexible and changeable, it provides powerful string parsing functions.

The sscanf prototype is simple and defined as follows:

# Include <stdio. h>
Int sscanf (const char * STR, const char * format ,...);

STR: string to be parsed

Format: string format description, followed by a pointer parameter with an indefinite number of sequences, storing parsed data

The following is a simple example:

Int year, month, day;

Int converted = sscanf ("20080808", "% 04d % 02d % 02d", & year, & month, & Day );

Printf ("converted = % d, year = % d, month = % d, Day = % d/N", converted, year, month, day );

Output result:

Converted = 3, year = 2008, month = 8, Day = 8

"% 04d % 02d % 02d" is used to parse the string format,%Indicates the start of format conversion,DReturns an integer,04AsDIt indicates that this is a four-digit integer. If it is less than four digits, it is filled with 0.

If the returned result is 3, three data records are successfully converted. The number of successfully converted data depends on the parsed string and its conversion format, if we change the format in this example to "% 04d % 02d", sscanf returns only 2, and the value of day is not changed by sscanf.

This is an example of Floating Point conversion:

Double longpolling, latitude;
Int converted = sscanf ("113.123456789 31.123456789", "% lf", & longpolling, & latitude );
Printf ("converted = % d, longpolling = %. 9lf, latitude = % lf/N", converted, longpolling, latitude );

Output result:

Converted = 2, longpolling = 113.123456789, latitude = 31.123457

In the sscanf Format String, F indicates that this is a floating point number, and its modifier L indicates that this is a double floating point number.

The above are two basic sscanf examples. Next we will demonstrate the advanced usage of sscanf:

Char STR [32] = "";
Sscanf ("123456 abcdedf", "% 31 [0-9]", STR );
Printf ("str = % s/n", STR );

Output result:

Str= 123456

In the above format, [0-9] indicates that this is a string containing only 0-9 characters, the above uses the 31 modifier to indicate the maximum length of the string buffer (this is also the most criticized part of sscanf, which is prone to buffer overflow errors. In fact, sscanf can avoid Buffer Overflow, when writing any string parsing format, pay attention to the limitations on its buffer size ).

Another example:

Char STR [32] = "";
Sscanf ("123456 abcdedf", "% 31 [0-9a-z]", STR );
Printf ("str = % s/n", STR );

Output result:

Str= 123456 abcdedf

The description of a-Z is added to the format.

Example of using ^:

Char STR [32] = "";
Sscanf ("123456 abcdedf", "% 31 [^ A-Z]", STR );
Printf ("str = % s/n", STR );

Output result:

Str= 123456

Add ^ in [] to indicate the opposite. The [^ A-Z] above indicates a string that does not contain any A-Z.

Example of using:

Char STR [32] = "";
Int ret = sscanf ("123456 abcdedf", "% * [0-9] % 31 [A-Z]", STR );
Printf ("ret = % d, STR = % s/n", RET, STR );

Output result:

Ret = 1, STR = abcdedf

Adding the * modifier indicates a neglected data, and you do not need to prepare a space for it to store the parsing results. In the preceding example, only one STR parameter is used to store the parsing result of % 31 [A-Z], and sscanf returns only 1, indicating that only one data is parsed.

After learning how to use [], ^, *, we will find that sscanf is such a powerful tool, many places that we originally thought must use regular expressions, it is very likely that sscanf can be used for implementation.

Below are some references for format:

Conversion Type:

% D: integer. A parameter of the int type * is required to store the conversion result.

% I: integer. If the string starts with 0x or 0x, it is converted in hexadecimal notation. If it starts with 0, it is converted in hexadecimal notation, otherwise, the conversion is in decimal order. A parameter of the int type * is required to store the conversion result.

% O: Unsigned octal number. A parameter of the unsigned int * type is required to store the conversion result.

% U: unsigned integer. A parameter of the unsigned int * type is required to store the conversion result.

% X: Unsigned hexadecimal number. A parameter of the unsigned int * type is required to store the conversion result.

% X: equivalent to % x

% F: floating point number. A float * type parameter is required to store the conversion result.

% C: a single character. A char * type parameter is required to store the conversion result.

% S: String ending with a space or line break. A char * type parameter is required to store the conversion result.

% []: A string that contains only characters in []. For example, [0-9] indicates a string of all numbers, [A-Z] indicates a string of all lowercase letters. A char * type parameter is required to store the conversion result.

% [^]: Opposite to the above, all the characters before [^,] are used as the end mark of the string. For example, [^ 0-9] indicates a string that does not contain any 0-9 characters, a char * type parameter is required to store the conversion result.

Conversion Type modifier:

*: Ignore flag. For example, % * D indicates that an integer is ignored, and % * s indicates that a string is ignored.

H: Short modifier. For example, % HD indicates that this is an integer of the short Int.

L: Long modifier. For example, % LD indicates that this is a long int integer, and % lf indicates that this is a double floating point number (% F indicates a float floating point number)

L: <GCC> long modifier. For example, % LD indicates a 64-bit integer, and % lf indicates a Long Double Floating Point.

I64: <VisualC ++> _ int64 modifier. For example, % i64d indicates that this is a 64-bit integer.

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.