The use of the sscanf () function

Source: Internet
Author: User
Tags uppercase letter

From: http://blog.csdn.net/tigerjibo/article/details/6442151

sscanf

Name:

SSCANF ()-Reads data in a string that matches the specified format.

Function Prototypes:

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

Header file:

#include <stdio.h>

Description

SSCANF is similar to scanf, which is used for input, except that the latter takes the keyboard (stdin) as the input source, the former with a fixed string as the input source.

Example:

1. Common usage.

Char buf[512];

SSCANF ("123456", "%s", buf);//Here buf is the name of the array, which means to deposit 123456 in buf in the form of%s!

printf ("%s/n", buf);

The result is: 123456

2. Take a string of the specified length. As in the following example, take a string with a maximum length of 4 bytes.

SSCANF ("123456", "%4s", buf);

printf ("%s/n", buf);

The result is: 1234

3. The string to take to the specified character. As in the following example, the string is encountered until the space is met.

SSCANF ("123456 Abcdedf", "%[^]", buf);

printf ("%s/n", buf);

The result is: 123456

4. Take a string that contains only the specified character set. As in the following example, take a string that contains only 1 to 9 and lowercase letters.

SSCANF ("123456abcdedfBCDEF", "%[1-9a-z]", buf);

printf ("%s/n", buf);

The result is: 123456ABCDEDF

When entering:

SSCANF ("123456abcdedfBCDEF", "%[1-9a-z]", buf);

printf ("%s/n", buf);

The result is: 123456

5. The string to be taken to the specified character set. As in the following example, take a string that encounters an uppercase letter.

SSCANF ("123456abcdedfBCDEF", "%[^a-z]", buf);

printf ("%s/n", buf);

The result is: 123456ABCDEDF

6, given a string iios/[email protected], gets/And the string between @, first filter out "iios/", and then send a string of non-' @ ' to BUF

SSCANF ("Iios/[email protected", "%*[^/]/%[^@]", buf);

printf ("%s/n", buf);

The result is: 12DDWDFF

7, given a string "" Hello, World ", only keep the world. (Note: "," followed by a space)

SSCANF ("Hello, World", "%*s%s", buf);

printf ("%s/n", buf);

The result: World

%*s indicates that the first match to the%s is filtered out, that is, Hello is filtered

If there are no spaces, the result is null.

The SSCANF function is very similar to regular expressions, but there is no strong regular expression, so it is recommended to use regular expressions for more complex string handling.

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

Use it to separate strings like this 2006:03:18:

int A, b, C;

/*SSCANF ("2006:03:18", "%d:%d:%d", A, B, c); */* Error method, to add the address before the variable a,b,c, modified by huanmie_09*/

SSCANF ("2006:03:18", "%d:%d:%d", &a, &b, &c);

and 2006:03:18-2006:04:18:

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

SSCANF ("2006:03:18-2006:04:18", "%s-%s", sztime1, sztime2);

But then, I need to deal with 2006:03:18-2006:04:18.

Just canceled the '-' on both sides of the space, but broke the%s of the definition of the string.

Do I need to redesign a function to handle such a situation? It's not complicated, but, in order to have a uniform style for all of the code, I need to change a lot of places to replace the existing sscanf with my own split function. I thought I must have done it, and slept with a strong dissatisfaction with sscanf, waking up and finding it unnecessary.

A type field such as%[] in Format-type. If you read a string that is not delimited by a space, you can use%[].

%[] is similar to a regular expression. [A-z] means reading all characters A-Z, [^a-z] means reading all characters except A-Z.

So the problem is solved:

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

In the Softmse (Jake) issue HTTP://COMMUNITY.CSD (remove me) n.n (remove me) et/expert/topic/4843/4843294.xml?temp=.4321558, Give a very cool sscanf use case, and then through learning, found sscanf awesome, now do a summary.

Original question:

Iios/[email protected]

Gets/And the string between @ how to do

Are there any functions in the C program?

Code:

#include <stdio.h>

int main ()

{

Const char* s = "iios/[email protected]";

Char buf[20];

SSCANF (S, "%*[^/]/%[^@]", buf);

printf ("%s/n", buf);

return 0;

}

The result is: 12DDWDFF

SSCANF is similar to scanf, which is used for input, except that the latter takes a screen (stdin) as the input source, the former with a fixed string as the input source.

Function Prototypes:

int scanf (const char *format [, argument] ...);

Where format can be one or more {%[*] [width] [{h | l | I64 | L}]type | ' | '/T ' | '/n ' | Non-% symbol},

Note: {A|b|c} indicates that a a,b,c is selected, [d], indicating that either D or D may not be available.

Width: wide, generally negligible, used such as:

const char sourcestr[] = "Hello, world";

char buf[10] =;

SSCANF (Sourcestr, "%5s", buf); %5s, take only 5 characters

cout << buf<< Endl;

The result is: Hello

{h | l | I64 | L}: The size of the parameter, usually h represents a single-byte size,i that represents a 4-byte size (double exception) for a 2-byte size,l, and L64 represents a 8-byte size.

Type: That's a lot, that's%s,%d.

In particular:

%*[width] [{h | l | I64 | L}]type indicates that the condition is filtered out and no value is written to the target parameter. Such as:

const char sourcestr[] = "Hello, world";

char buf[10] =;

SSCANF (Sourcestr, "%*s%s", buf); %*s indicates that the first match to the%s is filtered out, that is, Hello is filtered

cout << buf<< Endl;

The result: World

Support for collection operations:

%[a-z] to match any character in a to Z, greed (as many matches as possible)

%[ab ' matches a, B, ' one member, greed

%[^a] matches any character not a, greed

Does it look familiar? Yes, it's very similar to regular expressions, and still supports filtering, which can have%*[a-z]. For example:

Example review:

Const char* s = "iios/[email protected]";

Char buf[20];

SSCANF (S, "%*[^/]/%[^@]", buf);

printf ("%s/n", buf);

String from Example 3-"takes the specified character. As in the following example, the string is encountered until the space is met.

SSCANF ("123456 Abcdedf", "%[^]", buf);

printf ("%s/n", buf);

The result is: 123456

So the code summary should be:

Const char* s = "iios/[email protected]";

Char buf[20];

SSCANF (S, "%*[^/]/%[^@]", buf);

printf ("%s/n", buf);

First the "iios/" filter out, and then to the character ' @ ' up to a string of 12DDWDFF (by example 3 can get this string to @, the @122 out) content is: 12DDWDFF sent to buf, to get results.

The use of the sscanf () function

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.