Detailed C language sscanf () function, vsscanf () function, vscanf () function _c language

Source: Internet
Author: User
Tags string format

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

#include <stdio.h>

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

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

Argument parameter str is the string to read the data, format is the user-specified form, and argument is a variable to hold the read data.

"Return value" succeeds returns the number of parameters, failure returns 1, and the reason for the error exists in errno.

SSCANF () Converts the string of parameter str to the parameter format (formatted string) and formats the data (format string refer to scanf ()), and the converted result is stored in the corresponding variable.

SSCANF () is similar to scanf () and is used for input, except that scanf () is the input source with the keyboard (stdin), sscanf () with a fixed string as the input source.

Instance reads the integer 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 like a regular expression (of course there is no strong regular expression, and a complex string is recommended for regular expression processing) to support collection operations, such as:

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

In addition, format can not only define strings with a space, but also can be defined with other characters, you can achieve a simple string segmentation (more flexible string segmentation please use Strtok ()). For example:

  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
header file:

 #include <stdio.h>

To define a function:

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

Function Description: vsscanf () Converts the string of parameter str according to the parameter format string and formats the data. Please refer to the Appendix C or vprintf () example for format conversion.

Return value: Returns the number of parameters for success, failure returns 1, and the reason for the error is in errno.

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

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

To define a function:

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

Function Description: vscanf () converts the data entered according to the parameter format string and formats the data. Please refer to scanf () for the format conversion form. The converted results are stored in the corresponding parameters. Please refer to the Appendix C or vprintf () examples for va_list usage.
The return value succeeds returns the number of parameters, and the failure returns-1, the reason for the error is stored in errno.


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.