sscanf ()- 读取数据

格式

#include <stdio.h>
int sscanf(const char *buffer, const char *format, argument-list);

语言级别

ANSI

线程安全

语言环境敏感

此函数的行为可能受当前语言环境的 LC_CTYPE 和 LC_NUMERIC 类别影响。 如果在编译命令中指定了 LOCALETYPE (*LOCALEUCS2) 或 LOCALETYPE (*LOCALEUTF) ,那么此行为也可能受当前语言环境的 LC_UNI_CTYPE 类别影响。 有关更多信息,请参阅 了解 CCSID 和语言环境

描述

sscanf() 函数将数据从 buffer 读取到 argument-list提供的位置。 每个 自变量 都必须是指向类型与 format-string中的类型说明符相对应的变量的指针。

返回值

sscanf() 函数返回已成功转换和分配的字段数。 返回值不包含已读取但未分配的字段。

当在转换任何内容之前迂到字符串结束时,返回值为 EOF

示例

此示例使用 sscanf() 从字符串 tokenstring读取各种数据,然后显示该数据。

#include <stdio.h>
#include <stddef.h>
 
int main(void)
{
    char    *tokenstring = "15 12 14";
    char    *string = "ABC Z";
    wchar_t  ws[81];
    wchar_t  wc;
    int   i;
    float fp;
    char  s[81];
    char  c;

    /* Input various data                                        */
    /* In the first invocation of sscanf, the format string is   */
    /* "%s %c%d%f". If there were no space between %s and %c,    */
    /* sscanf would read the first character following the       */
    /* string, which is a blank space.                           */

    sscanf(tokenstring, "%s %c%d%f", s, &c, &i, &fp);
    sscanf(string, "%ls %lc", ws,&wc);

    /* Display the data */
    printf("\nstring = %s\n",s);
    printf("character = %c\n",c);
    printf("integer = %d\n",i);
    printf("floating-point number = %f\n",fp);
    printf("wide-character string = %S\n",ws);
    printf("wide-character = %C\n",wc);
}

/*****************  Output should be similar to:  *****************

string = 15
character = 1
integer = 2
floating-point number = 14.000000
wide-character string = ABC
wide-character = Z

*******************************************************************/
 

相关信息