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
*******************************************************************/