On the usage of sscanf in C language _c language

Source: Internet
Author: User
Tags mixed
Name:
sscanf ()-Reads data from a string that matches the specified format.
Copy Code code as follows:

Function Prototypes:
Int sscanf (String str, String fmt, mixed var1, mixed var2 ...);
int scanf (const char *format [, argument] ...);

Description:
SSCANF is similar to scanf and is used for input, except that the latter takes the screen (stdin) as the input source, with a fixed string as the input source.
The format can be one or more {%[*] [width] [{h | l | I64 | L}]type | ' ' | '/T ' | '/n ' | Non-% symbol}
Note:
1, * can also be used in the format, (that is,%*d and%*s) with an asterisk (*) indicates that skipping this data is not read. (that is, do not read this data into the parameter)
2. {A|b|c} indicates that A,b,c is selected, [d], that it can have D or not d.
3. Width represents the reading breadth.
4, {h | l | I64 | L}: The size of the parameter, usually H represents a single-byte Size,i 2 byte size,l represents 4 byte size (double exception), and L64 represents 8 byte size.
5, type: This is a lot of, is%s,%d and so on.
6, Special:%*[width] [{h | l | I64 | L}]type indicates that the condition is filtered out and the value is not written to the target parameter

Support for collection operations:
%[a-z] means to match any character in a to Z, greedy (as many matches as possible)
%[ab '] Match A, B, ' a member, greedy sex
%[^a] matches any character that is not a, greedy
Here is an example program
Copy Code code as follows:

/*****************************************************
* * NAME:SSCANF.C
* * Author:gzshun
* * version:1.0
* * DATE:2011-12
* * DESCRIPTION:SSCANF function
******************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void Sscanf_test (void);
static void Sscanf_test (void)
{
int ret;
Char *string;
int digit;
Char buf1[255];
Char buf2[255];
Char buf3[255];
Char buf4[255];
The simplest use of/*1.
string = "Beijing 123";
ret = sscanf (String, "%s%s%d", Buf1, Buf2, &digit);
printf ("1.string=%s\n", string);
printf ("1.ret=%d, buf1=%s, buf2=%s, digit=%d\n\n", ret, BUF1, buf2, digit);
/*
* * Execution Results:
**1.ret=3, Buf1=china, buf2=beijing, digit=123
* * You can see that the return value of the SSCANF is the number of parameters read
*/
/*2. Take a string of the specified length.
string = "123456789";
SSCANF (String, "%5s", BUF1);
printf ("2.string=%s\n", string);
printf ("2.buf1=%s\n\n", BUF1);
/*
* * Execution Results:
**2.buf1=12345
*/
/*3. The string to which the specified character is taken.
string = "123/456";
SSCANF (String, "%[^/]", BUF1);
printf ("3.string=%s\n", string);
printf ("3.buf1=%s\n\n", BUF1);
/*
* * Execution Results:
**3.buf1=123
*/
/*4. The string to which the specified character set is taken * *
string = "123abcABC";
SSCANF (String, "%[^a-z]", BUF1);
printf ("4.string=%s\n", string);
printf ("4.buf1=%s\n\n", BUF1);
/*
* * Execution Results:
**4.buf1=123abc
*/
/*5. Takes a string containing only the specified character set */
string = "0123abcABC";
SSCANF (String, "%[0-9]%[a-z]%[a-z]", Buf1, Buf2, BUF3);
printf ("5.string=%s\n", string);
printf ("5.buf1=%s, buf2=%s, buf3=%s\n\n", Buf1, Buf2, BUF3);
/*
* * Execution Results:
**5.buf1=123, BUF2=ABC, BUF3=ABC
*/
/*6. Gets the string in the middle of the specified character */
string = "ios<android>wp7";
SSCANF (String, "%*[^&LT;]&LT;%[^&GT;]", BUF1);
printf ("6.string=%s\n", string);
printf ("6.buf1=%s\n\n", BUF1);
/*
* * Execution Results:
**6.buf1=android
*/
/*7. Specifies the string to skip.
string = "Iosvsandroid";
SSCANF (String, "%[a-z]vs%[a-z]", BUF1, BUF2);
printf ("7.string=%s\n", string);
printf ("7.buf1=%s, buf2=%s\n\n", Buf1, BUF2);
/*
* * Execution Results:
**7.buf1=ios, Buf2=android
*/
/*8. Split a string separated by a character.
string = "android-iphone-wp7";
/*
* * string via '-', followed by the delimiter '-',
* * played a role in filtering, somewhat similar to the 7th
*/
SSCANF (String, "%[^-]-%[^-]-%[^-]", Buf1, Buf2, BUF3);
printf ("8.string=%s\n", string);
printf ("8.buf1=%s, buf2=%s, buf3=%s\n\n", Buf1, Buf2, BUF3);
/*
* * Execution Results:
**8.buf1=android, Buf2=iphone, buf3=wp7
*/
/*9. Extract Email Address * *
string = "email:beijing@sina.com.cn";
SSCANF (String, "%[^:]:%[^@]@%[^."). %s ", Buf1, Buf2, BUF3, BUF4);
printf ("9.string=%s\n", string);
printf ("9.buf1=%s, buf2=%s, buf3=%s, buf4=%s\n\n", Buf1, Buf2, BUF3, BUF4);
/*
* * Execution Results:
**9.buf1=email, Buf2=beijing, Buf3=sina, buf4=com.cn
*/
/*10. Filter out strings that you don't want to intercept or need--supplement,
* * After the% plus a * number, which represents filtering this string, does not read
*/
String = "Android iphone wp7";
SSCANF (String, "%s%*s%s", BUF1, BUF2);
printf ("10.string=%s\n", string);
printf ("10.buf1=%s, buf2=%s\n\n", Buf1, BUF2);
/*
* * Execution Results:
**10.android wp7
*/
}
int main (int argc, char **argv)
{
Sscanf_test ();

return 0;
}
/*
* * Test Program
* * Environment:
**linux ubuntu 2.6.32-24-generic-pae #39-ubuntu SMP Wed June 07:39:26 UTC i686 Gnu/linux
**GCC version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)
**
gzshun@ubuntu:~/c/sscanf$ gcc Sscanf.c-o sscanf
gzshun@ubuntu:~/c/sscanf$./sscanf
1.string=china Beijing 123
1.ret=3, Buf1=china, buf2=beijing, digit=123
2.string=123456789
2.buf1=12345
3.string=123/456
3.buf1=123
4.string=123abcabc
4.buf1=123abc
5.string=0123abcabc
5.buf1=0123, BUF2=ABC, BUF3=ABC
6.string=ios<android>wp7
6.buf1=android
7.string=iosvsandroid
7.buf1=ios, Buf2=android
8.string=android-iphone-wp7
8.buf1=android, Buf2=iphone, buf3=wp7
9.string=email:beijing@sina.com.cn
9.buf1=email, Buf2=beijing, Buf3=sina, buf4=com.cn
10.string=android iphone wp7
10.buf1=android, buf2=wp7
*/

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.