Sscanf () Practice

Source: Internet
Author: User

Sscanf ()-read data that matches the specified format from a string.

Function prototype:

Int sscanf (const char *, const char *,...);

Int sscanf (const char * buffer, const char * format, [argument]...);

Data stored in buffer

Format control string

Argument select string

Sscanf reads data from the buffer and writes the data back according to argument settings.

If the call succeeds, the number of parameters is returned. If the call fails, 0 is returned. The error cause is stored in errno.

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: "," is followed by a space. % s is stopped when a space is used, and * is added to ignore the first read string)

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.

The sscanf function is similar to a regular expression,
However, regular expressions are not powerful. Therefore, we recommend that you use regular expressions for complex string processing.

//-------------------------------------------------------

Use it to separate strings such as 2006: 03: 18:

Int A, B, C;

/* Sscanf ("200:0:18", "% d: % d", a, B, c); * // * the error method must be in the variables A, B, add an address character before C,
Modified by huanmie_09 */

Sscanf ("200:0:18", "% d: % d", & A, & B, & C );

And-2006: 04: 18:

Char sztime1 [16] = "", sztime2 [16] = "";

Sscanf ("2006:0:18-2006:04:18", "% s-% s", sztime1, sztime2 );

But later, I needed to handle

The space on both sides of '-' is canceled, but the % s definition of the string is broken.

I need to re-design a function to handle this situation? This is not complicated, but in order to make all the Code have a uniform style, I need to change many places and replace the existing sscanf with my own split function. I thought I must do this and fell asleep with a strong dissatisfaction with sscanf. I woke up and found that I didn't have.

The format-type has a type field such as %. If the string to be read is not separated by spaces, you can use % [].

% [] Is similar to a regular expression. [A-Z] indicates that all characters of A-Z are read, and [^ A-Z] indicates that all characters except a-Z are read.

That's why the problem was solved:

Sscanf ("2006:0:18-2006:04:18", "% [0-9,:]-% [0-9,:]", sztime1, sztime2 );

// ------- Not the creator of this section ---------------------------------------------------------------------------------------------------

Sscanf ("2006:0:18-2006:04:18", "% [^-]-% s", sztime1, sztime2 );

Description: % [^-] matching '-' is the string "" with a space character saved to sztime1 by default, so % s will input "-" To sztime2 by default, Which is why '-' is added before % S. The output is the same as the author, but easier to understand and read.

// Begin end -----------------

Post http://community.csd (remove me) N. N (remove me) et/expert/topic/4843/4843294 .xml? In temp =. 4321558, a cool sscanf use case is provided, and then the sscanf is awesome after learning.

Original problem:

Iios/12ddwdff @ 122

How to obtain the string between/and @?

Is there any function in the C program?

Code:

# 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.

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.