C language string formatting sscanf Function

Source: Internet
Author: User

I recently liked this function sscanf.

Int sscanf (constchar * buffer, constchar * Format [, argument]...);

In stdlib, scanf and printf have many versions:

Fscanf and fprintf format the input and output respectively from the stream.

Scanf and printf format input and output from standard devices.

Sscanf and sprintf format the input and output to the character buffer.

Apart from the source and purpose of the redirection, these functions are the most important and powerful functions supported by format.

The following uses sscanf as an example to describe the powerful functions of each parameter.

1. common format specifiers:

% D

A decimal integer.

% O

Octal integer 0.

% X

Hexadecimal integer 0x.

% D

A decimal integer. If it starts with 0, it is octal. If it starts with 0x, It is hexadecimal.

% F

Floating Point Number.

% C

Single character.

% S

A string. If % s is followed by % d, % s will only match all non-numeric characters. if it is followed by a % [], % s will match all the characters not in %. if it is followed by a common string text, % s will match all the characters until the first appearance of text.

% NS

Match a string with a length of N.

% [Characters]

A string contains characters in any characters list.

A minus sign can be used to indicate a range. For example, % [a-d] indicates a string that contains any A, B, and C (the pre-closed and post-open intervals ).

A ^ symbol indicates the meaning of negation and removal. For example, % [^ ABC] indicates any character except A, B, and C.

These symbols can be used in combination.

% {Format %}

Repeat this format as much as possible. The matching result is an array. For example, % {% d %} matches 0 or multiple integers.

%

It indicates a percent sign (%). It is similar to an escape character.

If a star (*) is placed between percent sign % and format specifier, for example, % * D, the function will only match the format specifier, the characters before the first matching specifier are ignored.

The sscanf function returns an integer (ranging from 0 to 100) that represents the percentage of successfully matched characters ).

If a match fails, sscanf returns the result immediately and the value of the parameter that does not match the result will not change.

Ii. Instances

1. A piece of code circulating on the internet, called Zhou Xingxing (I don't know who it is)

# Include <stdio. h>
Int main ()
{
Const char * s = "iios/12ddwdff @ 122 ";
Char Buf [20];
Sscanf (S, "% * [^/]/% [^ @]", Buf );

Printf ("% s \ n", Buf );
Return 0;
}
Result: 12 ddwdff.

Where

% * [^/], Matching the previous string from the start to the First '/' character. Because '*' exists, this part is ignored.

/, Match the backslash '/'

% [^ @] Matches all strings before the first '@', that is, 12 ddwdff

2. An example of a back-end program that is being compiled recently.

To parse assembly instruction sequences such as mov A and R0

# Include <stdio. h>
Int main ()
{

Char * line = "mov A, @ R0 \ n ";

Char temp [2] [10];

Sscanf (line, "% * [^] % [^,]", temp [0]); // operand

Sscanf (line, "% * [^,], % [^ \ n]", temp [1]); // operand @ R0

Return 0;

}

In this example, temp [0] matches a, temp [1] matches @ R0.

Sscanf (line, "% * [^] % [^,]", temp [0]);

First, based on % * [^], you can ignore the previous "mov", followed by a space, and finally match.

Sscanf (line, "% * [^,], % [^ \ n]", temp [1]); // operand @ R0

First, match and ignore ',', 'according to % * [^,], and then match until the end of the row.

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.