C语言中的分隔字符串函数sscanf

sscanf  以自定义格式解析字符串内容

备注:适用于解析文本数据库内容,即固定格式的字符串。
懒得写了,直接copy help文档,以备忘。
function
<cstdio>
int sscanf ( const char * str, const char * format, ...);

Read formatted data from string

Reads data from str and stores them according to the parameterformat into the locations given by the additional arguments. Locations pointed by each additional argument are filled with their corresponding type of value specified in theformat string.

Parameters

str
C string that the function processes as its source to retrieve the data.
format
C string that contains one or more of the following items:
  • Whitespace character: the function will read and ignore any whitespace characters (this includes blank spaces and the newline and tab characters) which are encountered before the next non-whitespace character. This includes any quantity of whitespace characters, or none.
  • Non-whitespace character, except percentage signs (%): Any character that is not either a whitespace character (blank, newline or tab) or part of a format specifier (which begin with a% character) causes the function to read the next character from str, compare it to this non-whitespace character and if it matches, it is discarded and the function continues with the next character offormat and str. If the character does not match, the function fails and returns.
  • Format specifiers: A sequence formed by an initial percentage sign (%) indicates a format specifier, which is used to specify the type and format of the data to be retrieved from thestr string and stored in the locations pointed by the additional arguments. A format specifier follows this prototype:

    [=%[*][width][modifiers]type=]

    where:

    * An optional starting asterisk indicates that the data is to be retrieved from thestr string but ignored, i.e. it is not stored in the corresponding argument.
    width Specifies the maximum number of characters to be read in the current reading operation
    modifiers Specifies a size different from int (in the case of d, i and n), unsigned int (in the case ofo, u and x) or float (in the case of e,f and g) for the data pointed by the corresponding additional argument:
    h : short int (for d, i and n), orunsigned short int (for o, u and x)
    l : long int (for d, i and n), orunsigned long int (for o, u and x), or double (fore, f and g)
    L : long double (for e, f and g)
    type A character specifying the type of data to be read and how it is expected to be read. See next table.

    sscanf type specifiers:
    type Qualifying Input Type of argument
    c Single character: Reads the next character. If awidth different from 1 is specified, the function reads width characters and stores them in the successive locations of the array passed as argument. No null character is appended at the end. char *
    d Decimal integer: Number optionally preceeded with a+ or - sign. int *
    e,E,f,g,G Floating point: Decimal number containing a decimal point, optionally preceeded by a+ or - sign and optionally folowed by the e or E character and a decimal number. Two examples of valid entries are -732.103 and 7.12e4 float *
    o Octal integer. int *
    s String of characters. This will read subsequent characters until a whitespace is found (whitespace characters are considered to be blank, newline and tab). char *
    u Unsigned decimal integer. unsigned int *
    x,X Hexadecimal integer. int *
additional arguments
The function expects a sequence of references as additional arguments, each one pointing to an object of the type specified by their corresponding%-tag within the format string, in the same order.
For each format specifier in the format string that retrieves data, an additional argument should be specified.
These arguments are expected to be references (pointers): if you want to store the result of asscanf operation on a regular variable you should precede its identifier with thereference operator, i.e. an ampersand sign (&), like in:
int n;
sscanf (str,"%d",&n);

 

Return Value

On success, the function returns the number of items succesfully read. This count can match the expected number of readings or fewer, even zero, if a matching failure happens.
In the case of an input failure before any data could be successfully read,
EOF is returned.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/* sscanf example */ #include <stdio.h> int main () { char sentence []="Rudolph is 12 years old"; char str [20]; int i; sscanf (sentence,"%s %*s %d",str,&i); printf ("%s -> %d/n",str,i); return 0; }

你可能感兴趣的:(c,function,语言,character,whitespace,newline)