[Turn] use sscanf

Attach the original address:
https://www.cnblogs.com/hejing-swust/p/7793958.html
 1. common usage.

charstr[512]={0};
  sscanf("123456","%s",str);
  printf("str=%s",str);

2. Take the string length specified. As the following example, taking the maximum length of 4 bytes string.

sscanf("123456","%4s",str);
  printf("str=%s",str);

3. Take up the specified character string. As the following example, the space taken up encountered string.

sscanf("123456abcdedf","%[^]",str);
  printf("str=%s",str);

4. Take the string contains only the specified character set. As the following example, take 1-9 and contains only lowercase string.

sscanf("123456abcdedfBCDEF","%[1-9a-z]",str);
  printf("str=%s",str);

5. Take the string until the specified character set. As the following example, we encounter a string taken up capitalization.

sscanf("123456abcdedfBCDEF","%[^A-Z]",str);
  printf("str=%s",str);


The following code can be used to convert the ip address is a string of four integers:

char * inputIp
int IP [. 4];
sscanf_s (inputIp,. "..% D% D% D% D", & IP [0], & IP [. 1], & IP [2], & IP [. 3]);
Note sscanf_s when read is an integer type or other type can be determined length of time, can not keep up with the type of length in the back, but for a string type (char *) can not know the length must be clear that the maximum length of the string after the type (i.e., the space may be accommodated).

Guess you like

Origin blog.csdn.net/weixin_34194702/article/details/91012113