The usage of sscanf in C language

Source: Internet
Author: User
Tags character set function prototype mixed printf
The following is a detailed analysis of the use of the SSCANF function in C language, the need for friends under the reference

Name:
sscanf ()-Reads data from a string that matches the specified format.

Copy Code code as follows:


function Prototype:
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];


/*1. The simplest usage/


string = "Beijing 123";


ret = sscanf (string,%s%s%d, buf1, BUF2, &digit);


printf ("1.string=%sn", string);


printf ("1.ret=%d, buf1=%s, buf2=%s, Digit=%dnn", ret, BUF1, buf2, digit);


 /*


* Execution Results:


**1.ret=3, Buf1=china, buf2=beijing, digit=123


* Can see that the return value of SSCANF is the number of parameters read


 */


/*2. Take the specified length of the string/


string = "123456789";


sscanf (String, "%5s", BUF1);


printf ("2.string=%sn", string);


printf ("2.buf1=%snn", BUF1);


 /*


* Execution Results:


**2.buf1=12345


 */


/*3. String to the specified character * *


string = "123/456";


sscanf (String, "%[^/]", BUF1);


printf ("3.string=%sn", string);


printf ("3.buf1=%snn", BUF1);


 /*


* Execution Results:


**3.buf1=123


 */


/*4. String to the specified character set * *


string = "123abcABC";


sscanf (String, "%[^a-z]", BUF1);


printf ("4.string=%sn", string);


printf ("4.buf1=%snn", 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=%sn", string);


printf ("5.buf1=%s, buf2=%s, Buf3=%snn", 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, "%*[^<]<%[^>]", BUF1);


printf ("6.string=%sn", string);


printf ("6.buf1=%snn", 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=%sn", string);


printf ("7.buf1=%s, Buf2=%snn", 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 delimiter '-',


* * played a role in filtration, somewhat similar to the 7th


 */


sscanf (String, "%[^-]-%[^-]-%[^-]", Buf1, Buf2, BUF3);


printf ("8.string=%sn", string);


printf ("8.buf1=%s, buf2=%s, Buf3=%snn", 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=%sn", string);


printf ("9.buf1=%s, buf2=%s, buf3=%s, Buf4=%snn", 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,


* * Adds a * number after the%, which represents filtering this string and does not read


 */


string = "android iphone wp7";


sscanf (String, "%s%*s%s", BUF1, BUF2);


printf ("10.string=%sn", string);


printf ("10.buf1=%s, Buf2=%snn", 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.