Special sscanf/scanf usage

Source: Internet
Author: User

% []Usage: % [] indicates to read a character set combination. If [followed by the first character is "^", it indicates the inverse meaning.

The string in [] can be 1 or more characters. The null character set (% []) is in violation of regulations and can

Result in unpredictable results. % [^] Is also in violation of regulations.

% [A-Z]Reads a string between a-Z. If it is not before, it is stopped, as shown in figure

Char s [] = "Hello, my friend"; // note: the comma is between non-a-Z.

Sscanf (S, "% [A-Z]", string); // string = Hello

% [^ A-Z]Reads strings not between A and Z. If a character between A and Z is encountered, it is stopped, as shown in figure

Char s [] = "hellokitty"; // note: the comma is not between a and Z.

Sscanf (S, "% [^ A-Z]", string); // string = Hello

% * [^ =]Front band*No. indicates that no variable is saved. Skips a qualified string.

Char s [] = "Notepad = ";

Char szfilename [32] = "";

Int I = sscanf (S, "% * [^ =]", szfilename); // szfilename = NULL because it is not saved

Int I = sscanf (S, "% * [^ =] = % s", szfilename); // szfilename =

% 40CRead 40 characters

The run-time

Library does not automatically append a null Terminator

To the string, nor does reading 40 characters

Automatically terminate the scanf () function. Because

Library uses buffered input, you must press the Enter key

To terminate the string scan. If you press the enter before

The scanf () reads 40 characters, it is displayed normally,

And the Library continues to prompt for additional input

Until it reads 40 characters

% [^ =]Read the string until it reaches the '=' sign. '^' can contain more characters, such:

Char s [] = "Notepad = ";

Char szfilename [32] = "";

Int I = sscanf (S, "% [^ =]", szfilename); // szfilename = notepad

If the parameter format is: % [^ =:], you can also read notepad from notepad:


Char s [] = "Notepad = ";

Char szname [32] = "";

Char szver [32] = "";

Sscanf (S, "% [^ =] = % s", szname, szver); // szname = notepad, szver =

Summary: % [] has many functions, but it is not very common, mainly because:

1. Many system scanf functions have vulnerabilities. (In typical cases, TC sometimes fails to input floating point functions ).

2. Complicated usage and error-prone.

3. It is difficult for the compiler to perform syntax analysis, thus affecting the quality and execution efficiency of the target code.

I personally think that 3rd is the most critical, and the more complicated the functions, the lower the execution efficiency. Some simple string analysis can be processed by ourselves.

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.