zoukankan      html  css  js  c++  java
  • scanf

    manual:

    http://man7.org/linux/man-pages/man3/scanf.3.html

    http://www.cnblogs.com/jcss2008/archive/2008/09/20/1294905.html

    int scanf( const char *format, ... );

     函数 scanf() 是从标准输入流 stdin 中读内容的通用子程序,可以读入全部固有类型的数据并自动转换成机内形式。
        在 C99 中,format 用 restrict 修饰。
    format 指向的控制串由以下三类字符组成:
           ● 格式说明符
           ● 空白符
           ● 非空白符
         
         转换字符(就是%后跟的部分)              
           a   读浮点值(仅适用于 C99)                                  
           A   读浮点值(仅适用于 C99)                                                  
           c   读单字符                                                
           d   读十进制整数                                            
           i   读十进制、八进制、十六进制整数                          
           e   读浮点数                                                
           E   读浮点数                                                
           f   读浮点数                                                
           F   读浮点数(仅适用于 C99)                                  
           g   读浮点数                                                
           G   读浮点数                                                
           o   读八进制数                                              
           s   读字符串                                                
           x   读十六进制数                                            
           X   读十六进制数                                            
           p   读指针值                                                
           n   至此已读入值的等价字符数                                
           u   读无符号十进制整数                                      
          [ ]  扫描字符集合                                            
           %   读 % 符号(百分号)    

              

        int t1, t2;
        int ret = sscanf("123-12","%d-%d", &t1, &t2);
        //ret = 2, t1 = 123, t2 = 12
    
        char buf1[256],buf2[256];
        ret = sscanf("love is520 verb!","%s %[a-z]", buf1, buf2);
        //ret = 2, buf1= "love", buf2 = "is"
    
        ret = sscanf("love 23is520 verb!","%s %[^0]", buf1, buf2);
        //ret = 2, buf1= "love", buf2 = "23is52"
    
        char buf3[256];
        int t3;
        ret = sscanf("love is 502","%s %*s %d", buf3, &t3);
        //ret = 2, buf1= "love", t3 = 502
    
        char c;
        ret = sscanf(" 222","%c%d", &c, &t1);
        //ret = 2, c=' ' t1 = 222;
    
        char buf4[256];
        int t4;
        ret = sscanf("loveisverb520! 886 ", "%10s%d", buf4, & t4);
        //ret = 2, buf4 = "loveisverb" t4 = 520
    1. 首先读入一个%d类型的数据,如果和第一个参数匹配,则继续从缓冲区中读取数据和第二个参数进行匹配,依次进行下去,直到匹配完所有的参数;如果其中有一个参数不匹配,那就从这个地方跳出,忽略这个scanf后面所有的参。匹配成功返回参数个数,匹配失败返回EOF。
    2. 在输入流中,数据项必须由空格、制表符和新行符分割。逗号和分号等不是分隔符。控制串中的空白符使 scanf() 在输入流中跳过一个或多个空白行。空白符可以是空格(space)、制表符(tab)和新行符(newline)。 本质上,控制串中的空白符使 scanf() 在输入流中读,但不保存结果,直到发现非空白字符为止。非空白符使 scanf() 在流中读一个匹配的字符并忽略之。例如,"%d,%d" 使 scanf() 先读入一个整数,读入中放弃逗号,然后读另一个整数。如未发现匹配,scanf() 返回。 scanf() 中用于保存读入值的变元必须都是变量指针,即相应变量的地址。
    3.  ANSI C 标准向 scanf() 增加了一种新特性,称为扫描集(scanset)。 扫描集定义一个字符集合,可由 scanf() 读入其中允许的字符并赋给对应字符数组。 扫描集合由一对方括号中的一串字符定义,左方括号前必须缀以百分号。 例如,以下的扫描集使 scanf() 读入字符 A、B 和 C: %[ABC]。  使用扫描集时,scanf() 连续吃进集合中的字符并放入对应的字符数组,直到发现不在集合中的字符为止(即扫描集仅读匹配的字符)。返回时,数组中放置以 null 结尾、由读入字符组成的字符串。   用字符 ^ 可以说明补集。把 ^ 字符放为扫描集的第一字符时,构成其它字符组成的命令的补集合,指示 scanf() 只接受未说明的其它字符。    对于许多实现来说,用连字符可以说明一个范围。 例如,以下扫描集使 scanf() 接受字母 A 到 Z:    %[A-Z]   重要的是要注意扫描集是区分大小写的。因此,希望扫描大、小写字符时,应该分别说明大、小写字母。

    4.  百分号(%)与格式符之间的星号(*)表示读指定类型的数据但不保存。因此, scanf( "%d %*c %d", &x, &y );对 10/20 的读入操作中,10 放入变量 x,20 放入 y。 格式命令可以说明最大域宽。 在百分号(%)与格式码之间的整数用于限制从对应域读入的最大字符数。例如,希望向 address 读入不多于 20 个字符时,可以书写成如下形式:                   scanf( "%20s", address );   如果输入流的内容多于 20 个字符,则下次 scanf() 从此次停止处开始读入。 若达到最大域宽前已遇到空白符,则对该域的读立即停止;此时,scanf() 跳到下一个域。

    5.  虽然空格、制表符和新行符都用做域分割符号,但读单字符操作中却按一般字符处理。
    int scanf ( const char * format, ... );
    Read formatted data from stdin

    Reads data from stdin and stores them according to the parameter format into the locations pointed by the additional arguments.

    The additional arguments should point to already allocated objects of the type specified by their corresponding format specifier within the format string.

    Parameters

    format
    C string that contains a sequence of characters that control how characters extracted from the stream are treated:
    • Whitespace character: the function will read and ignore any whitespace characters encountered before the next non-whitespace character (whitespace characters include spaces, newline and tab characters -- see isspace). A single whitespace in the format string validates any quantity of whitespace characters extracted from the stream (including none).
    • Non-whitespace character, except format specifier (%): 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 the stream, compare it to this non-whitespace character and if it matches, it is discarded and the function continues with the next character of format. If the character does not match, the function fails, returning and leaving subsequent characters of the stream unread.
    • 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 the stream and stored into the locations pointed by the additional arguments.

    format specifier for scanf follows this prototype:

    %[*][width][length]specifier 

    Where the specifier character at the end is the most significant component, since it defines which characters are extracted, their interpretation and the type of its corresponding argument:
    specifierDescriptionCharacters extracted
    i Integer Any number of digits, optionally preceded by a sign (+ or -).
    Decimal digits assumed by default (0-9), but a 0 prefix introduces octal digits (0-7), and 0x hexadecimal digits (0-f).
    Signed argument.
    d or u Decimal integer Any number of decimal digits (0-9), optionally preceded by a sign (+ or -).
    d is for a signed argument, and u for an unsigned.
    o Octal integer Any number of octal digits (0-7), optionally preceded by a sign (+ or -).
    Unsigned argument.
    x Hexadecimal integer Any number of hexadecimal digits (0-9a-fA-F), optionally preceded by 0x or 0X, and all optionally preceded by a sign (+ or -).
    Unsigned argument.
    feg Floating point number A series of decimal digits, optionally containing a decimal point, optionally preceeded by a sign (+ or -) and optionally followed by the e or E character and a decimal integer (or some of the other sequences supported by strtod).
    Implementations complying with C99 also support hexadecimal floating-point format when preceded by 0x or 0X.
    a
    c Character The next character. If a width other than 1 is specified, the function reads exactlywidth characters and stores them in the successive locations of the array passed as argument. No null character is appended at the end.
    s String of characters Any number of non-whitespace characters, stopping at the first whitespacecharacter found. A terminating null character is automatically added at the end of the stored sequence.
    p Pointer address A sequence of characters representing a pointer. The particular format used depends on the system and library implementation, but it is the same as the one used to format %p in fprintf.
    [characters] Scanset Any number of the characters specified between the brackets.
    A dash (-) that is not the first character may produce non-portable behavior in some library implementations.
    [^characters] Negated scanset Any number of characters none of them specified as characters between the brackets.
    n Count No input is consumed.
    The number of characters read so far from stdin is stored in the pointed location.
    % % % followed by another % matches a single %.
    Except for n, at least one character shall be consumed by any specifier. Otherwise the match fails, and the scan ends there.

    The format specifier can also contain sub-specifiers: asterisk (*), width and length (in that order), which are optional and follow these specifications:
    sub-specifierdescription
    * An optional starting asterisk indicates that the data is to be read from the stream but ignored (i.e. it is not stored in the location pointed by an argument).
    width Specifies the maximum number of characters to be read in the current reading operation (optional).
    length One of hhhllljztL (optional).
    This alters the expected type of the storage pointed by the corresponding argument (see below).

    This is a chart showing the types expected for the corresponding arguments where input is stored (both with and without a length sub-specifier):
     specifiers
    lengthd iu o xf e g ac s [] [^]pn
    (none) int* unsigned int* float* char* void** int*
    hh signed char* unsigned char*       signed char*
    h short int* unsigned short int*       short int*
    l long int* unsigned long int* double* wchar_t*   long int*
    ll long long int* unsigned long long int*       long long int*
    j intmax_t* uintmax_t*       intmax_t*
    z size_t* size_t*       size_t*
    t ptrdiff_t* ptrdiff_t*       ptrdiff_t*
    L     long double*      
    Note: Yellow rows indicate specifiers and sub-specifiers introduced by C99.
    ... (additional arguments)
    Depending on the format string, the function may expect a sequence of additional arguments, each containing a pointer to allocated storage where the interpretation of the extracted characters is stored with the appropriate type.
    There should be at least as many of these arguments as the number of values stored by the format specifiers. Additional arguments are ignored by the function.
    These arguments are expected to be pointers: to store the result of a scanf operation on a regular variable, its name should be preceded by the reference operator (&) (see example).

    Return Value

    On success, the function returns the number of items of the argument list successfully filled. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of theend-of-file.

    If a reading error happens or the end-of-file is reached while reading, the proper indicator is set (feof or ferror). And, if either happens before any data could be successfully read, EOF is returned.

    If an encoding error happens interpreting wide characters, the function sets errno to EILSEQ.

  • 相关阅读:
    bzoj 2456 mode
    codeforces 630 I(规律&&组合)
    codeforces 630H (组合数学)
    codeforces 651A Joysticks
    codeforces 651B Beautiful Paintings
    codeforces 625C K-special Tables
    codeforces 630F Selection of Personnel(组合数)
    codeforce 630N Forecast
    后缀数组模板
    Hdu5737-Differencia(有序表线段树)
  • 原文地址:https://www.cnblogs.com/iclk/p/3973334.html
Copyright © 2011-2022 走看看