sscanf、_sscanf_l、swscanf、_swscanf_lsscanf, _sscanf_l, swscanf, _swscanf_l

从字符串中读取格式化数据。Read formatted data from a string. 这些函数的更安全版本已发布;请参阅 sscanf_s、_sscanf_s_l、swscanf_s、_swscanf_s_lMore secure versions of these functions are available; see sscanf_s, _sscanf_s_l, swscanf_s, _swscanf_s_l.

语法Syntax

int sscanf(
   const char *buffer,
   const char *format [,
   argument ] ...
);
int _sscanf_l(
   const char *buffer,
   const char *format,
   locale_t locale [,
   argument ] ...
);
int swscanf(
   const wchar_t *buffer,
   const wchar_t *format [,
   argument ] ...
);
int _swscanf_l(
   const wchar_t *buffer,
   const wchar_t *format,
   locale_t locale [,
   argument ] ...
);

参数Parameters

bufferbuffer
存储的数据Stored data

formatformat
窗体控件字符串。Format-control string. 有关更多信息,请参见 格式规范For more information, see Format Specifications.

自变量argument
可选自变量Optional arguments

localelocale
要使用的区域设置The locale to use

返回值Return Value

每个函数都将返回成功转换并分配的字段数;返回值不包括已读取但未分配的字段。Each of these functions returns the number of fields successfully converted and assigned; the return value does not include fields that were read but not assigned. 返回值为 0 表示没有分配任何字段。A return value of 0 indicates that no fields were assigned. 返回值是EOF错误或如果在第一个转换之前到达字符串的末尾。The return value is EOF for an error or if the end of the string is reached before the first conversion.

如果缓冲区格式NULL调用指针,无效参数处理程序,如中所述参数验证If buffer or format is a NULL pointer, the invalid parameter handler is invoked, as described in Parameter Validation. 如果允许执行继续,这些函数将返回-1 并设置errnoEINVALIf execution is allowed to continue, these functions return -1 and set errno to EINVAL.

有关这些代码及其他错误代码的信息,请参阅 _doserrno、errno、_sys_errlist 和 _sys_nerrFor information on these and other error codes, see _doserrno, errno, _sys_errlist, and _sys_nerr.

备注Remarks

Sscanf函数读取来自数据缓冲区到每个指定的位置参数The sscanf function reads data from buffer into the location given by each argument. 每个自变量必须是指向具有中的类型说明符相对应的类型的变量的指针格式Every argument must be a pointer to a variable with a type that corresponds to a type specifier in format. 格式参数控制字段输入的解释,并且具有相同格式和函数作为格式参数scanf函数。The format argument controls the interpretation of the input fields and has the same form and function as the format argument for the scanf function. 如果复制出现在重叠的字符串之间,则该行为不确定。If copying takes place between strings that overlap, the behavior is undefined.

重要

读取的字符串时sscanf,始终指定的宽度 %s格式 (例如, "%32 秒" 而不是 "%s"); 否则为输入格式不正确很容易导致缓冲区溢出。When reading a string with sscanf, always specify a width for the %s format (for example, "%32s" instead of "%s"); otherwise, improperly formatted input can easily cause a buffer overrun.

swscanf是宽字符版本sscanf; 的自变量swscanf都是宽字符字符串。swscanf is a wide-character version of sscanf; the arguments to swscanf are wide-character strings. sscanf不处理多字节十六进制字符。sscanf does not handle multibyte hexadecimal characters. swscanf不处理 Unicode 全角十六进制或"兼容区"字符。swscanf does not handle Unicode full-width hexadecimal or "compatibility zone" characters. 否则为swscanfsscanf行为方式相同。Otherwise, swscanf and sscanf behave identically.

使用这些函数的版本 _l后缀完全相同,只不过它们使用传递中而不是当前线程区域设置的区域设置参数。The versions of these functions with the _l suffix are identical except that they use the locale parameter passed in instead of the current thread locale.

一般文本例程映射Generic-Text Routine Mappings

TCHAR.H 例程TCHAR.H routine 未定义 _UNICODE 和 _MBCS_UNICODE & _MBCS not defined 已定义 _MBCS_MBCS defined 已定义 _UNICODE_UNICODE defined
_stscanf_stscanf sscanfsscanf sscanfsscanf swscanfswscanf
_stscanf_l_stscanf_l _sscanf_l_sscanf_l _sscanf_l_sscanf_l _swscanf_l_swscanf_l

要求Requirements

例程所返回的值Routine 必需的标头Required header
sscanf_sscanf_lsscanf, _sscanf_l <stdio.h><stdio.h>
swscanf_swscanf_lswscanf, _swscanf_l <stdio.h> 或 <wchar.h><stdio.h> or <wchar.h>

有关其他兼容性信息,请参阅 兼容性For additional compatibility information, see Compatibility.

示例Example

// crt_sscanf.c
// compile with: /W3
// This program uses sscanf to read data items
// from a string named tokenstring, then displays them.

#include <stdio.h>

int main( void )
{
   char  tokenstring[] = "15 12 14...";
   char  s[81];
   char  c;
   int   i;
   float fp;

   // Input various data from tokenstring:
   // max 80 character string:
   sscanf( tokenstring, "%80s", s ); // C4996
   sscanf( tokenstring, "%c", &c );  // C4996
   sscanf( tokenstring, "%d", &i );  // C4996
   sscanf( tokenstring, "%f", &fp ); // C4996
   // Note: sscanf is deprecated; consider using sscanf_s instead

   // Output the data read
   printf( "String    = %s\n", s );
   printf( "Character = %c\n", c );
   printf( "Integer:  = %d\n", i );
   printf( "Real:     = %f\n", fp );
}
String    = 15
Character = 1
Integer:  = 15
Real:     = 15.000000

请参阅See also

流 I/OStream I/O
fscanf、_fscanf_l、fwscanf、_fwscanf_lfscanf, _fscanf_l, fwscanf, _fwscanf_l
scanf、_scanf_l、wscanf、_wscanf_lscanf, _scanf_l, wscanf, _wscanf_l
sprintf、_sprintf_l、swprintf、_swprintf_l、__swprintf_lsprintf, _sprintf_l, swprintf, _swprintf_l, __swprintf_l
snprintf、_snprintf、_snprintf_l、_snwprintf、_snwprintf_lsnprintf, _snprintf, _snprintf_l, _snwprintf, _snwprintf_l