A brief analysis of the use of sscanf in C language

  • 2020-04-02 01:07:25
  • OfStack

Name:
Sscanf () - reads data from a string that matches the specified format.

 The function prototype :
Int  sscanf( string str, string fmt, mixed var1, mixed var2 ... );
int scanf( const char *format [,argument]... );

Description:
Sscanf is similar to scanf in that it is used for input, except that the latter takes the screen (stdin) as the input source and the former takes a fixed string as the input source.
The format can be one or more {% [*] [width] [{h | l | I64 | l}] type | '|'/t '| | the % symbol}'/n '
Note:
1, * can also be used in the format, (that is, %*d and %*s) with an asterisk (*) to indicate skipping over the data and not reading it.
2. {a|,b |c} means a,b,c choose one, [d], means there can be d or there can be no d.
3. Width represents the read width.
4. {h | l | I64 | l}: size of parameter, usually h represents the size of a single byte, I represents the size of 2 bytes,l represents the size of 4 bytes (double exception), and l64 represents the size of 8 bytes.
5, type: that's a lot, just %s,%d and so on.
6. Special: %*[width] [{h | l | I64 | l}]type indicates that those meeting this condition are filtered out, and the value is not written to the target parameter

Support for collection operations:
        %[a-z] denotes any character from a to z, cupidity (as many matches as possible)
        %[aB'] matches a, B, and ', cupidity
        %[^a] matches arbitrary characters that are not a, cupidity
The following is an example program


#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];
 
 string = "china 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);
 
 
 string = "123456789";
 sscanf(string, "%5s", buf1);
 printf("2.string=%sn", string);
 printf("2.buf1=%snn", buf1);
 
 
 string = "123/456";
 sscanf(string, "%[^/]", buf1);
 printf("3.string=%sn", string);
 printf("3.buf1=%snn", buf1);
 
 
 string = "123abcABC";
 sscanf(string, "%[^A-Z]", buf1);
 printf("4.string=%sn", string);
 printf("4.buf1=%snn", buf1);
 
 
 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);
 
 
 string = "ios<android>wp7";
 sscanf(string, "%*[^<]<%[^>]", buf1);
 printf("6.string=%sn", string);
 printf("6.buf1=%snn", buf1);
 
 
 string = "iosVSandroid";
 sscanf(string, "%[a-z]VS%[a-z]", buf1, buf2);
 printf("7.string=%sn", string);
 printf("7.buf1=%s, buf2=%snn", buf1, buf2);
 
 
 string = "android-iphone-wp7";
 
 sscanf(string, "%[^-]-%[^-]-%[^-]", buf1, buf2, buf3);
 printf("8.string=%sn", string);
 printf("8.buf1=%s, buf2=%s, buf3=%snn", buf1, buf2, buf3);
 
 
 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);
 
 
 string = "android iphone wp7";
 sscanf(string, "%s %*s %s", buf1, buf2);
 printf("10.string=%sn", string);
 printf("10.buf1=%s, buf2=%snn", buf1, buf2);
 
}
int main(int argc, char **argv)
{
 sscanf_test();

 return 0;
}
/*
** The test program 
** The environment :
**Linux ubuntu 2.6.32-24-generic-pae #39-Ubuntu SMP Wed Jul 28 07:39:26 UTC 2010 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 articles: