C language sscanf of function vsscanf of function vscanf of function

  • 2020-04-02 03:18:22
  • OfStack

C language sscanf() function: reads data in a specified format from a string
The header file:


#include <stdio.h>

The sscanf() function is used to read data in the specified format from a string, and its prototype is as follows:
     


 int sscanf (char *str, char * format [, argument, ...]);

[parameter] the parameter STR is the string to read the data; Format is the format specified by the user; Argument is the variable that holds the data that you've read.

[return value] returns the number of parameters on success, -1 on failure, and the reason for the error is in errno.

Sscanf () converts and formats the string of the parameter STR according to the parameter format (see scanf() for the format string), and the converted result is stored in the corresponding variable.

Sscanf () is similar to scanf() in that it is used for input, except that scanf() takes the keyboard (stdin) as the input source and sscanf() takes a fixed string as the input source.

Reads integers and lowercase letters from the specified string.


#include <stdio.h>
int main(void)
{
  char str[100] ="123568qwerSDDAE";
  char lowercase[100];
  int num;
  sscanf(str,"%d %[a-z]", &num, lowercase);
  printf("The number is: %d.n", num);
  printf("The lowercase is: %s.", lowercase);
  return 0;
}

Output results:


The number is: 123568.
The lowercase is: qwer.

You can see that the format parameter is somewhat similar to regular expressions (not as powerful as regular expressions, of course, complex strings are recommended to use regular expressions) and supports collection operations, such as:

      %[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

In addition, format can not only delimit a string with a space, but also with other characters, can achieve a simple string segmentation (more flexible string segmentation please use strtok()). Such as:


  sscanf("2006:03:18", "%d:%d:%d", a, b, c);
  sscanf("2006:03:18 - 2006:04:18", "%s - %s", sztime1, sztime2);

C language vsscanf() function: string input function
The header file:


 #include <stdio.h>

Definition function:


int vsscanf(const char * str, const char * format, va_list ap);

Vsscanf () converts and formats the data from the string of parameter STR according to the parameter format string. Please refer to appendix C or vprintf() for the format conversion form.

Return value: returns the number of arguments on success, -1 on failure, and the reason for the error is in errno.

C language vscanf() function: string format input function
The header file:


#include <stdio.h>  #include <stdarg.h>

Definition function:


int vscanf(const char * format, va_list ap);

Vscanf () will convert and format the input data according to the parameter format string. Please refer to scanf() for the format conversion form. The converted results are stored in the corresponding parameter. Please refer to appendix C or vprintf() for the usage of va_list.
The return value returns the number of parameters on success, -1 on failure, and the reason for the error is in errno.



Related articles: