sscanf_s、 _sscanf_s_l、 swscanf_s、 _swscanf_s_l

讀取的格式字串中的資料。這些是舊版sscanf、 _sscanf_l、 swscanf、 _swscanf_l中所述的安全性增強功能與安全性功能,則在 CRT 中

int sscanf_s(
   const char *buffer,
   const char *format [,
   argument ] ...
);
int _sscanf_s_l(
   const char *buffer,
   const char *format,
   locale_t locale [,
   argument ] ...
);
int swscanf_s(
   const wchar_t *buffer,
   const wchar_t *format [,
   argument ] ...
);
int _swscanf_s_l(
   const wchar_t *buffer,
   const wchar_t *format,
   locale_t locale [,
   argument ] ...
);

參數

  • buffer
    預存的資料

  • format
    控制項的格式字串。如需詳細資訊,請參閱格式規格

  • argument
    選擇性引數

  • locale
    若要使用的地區設定

傳回值

每個函式傳回的欄位順利轉換,並指派; 傳回的值不包括讀取但未指定的欄位。傳回值 0 表示沒有欄位被指定。傳回值是EOF的錯誤,或是第一個轉換之前先到達字串的結尾。

如果buffer或format是NULL指標,不正確的參數處理常式會叫用,如所述參數驗證。如果執行,則允許繼續執行,這些函數會傳回-1,並設定errno ,EINVAL

有關這些及其他錯誤碼資訊,請參閱 _doserrno、 errno、 _sys_errlist,以及 _sys_nerr

備註

sscanf_s 函式會將資料從buffer到每個給定的位置argument。之後的格式字串引數會對應到的型別規範中的型別指定給變數的指標format。與安全性較低的版本不同的是sscanf,緩衝區大小參數時,需要使用相同的型別欄位字元c, C, s, S 和[。每一個緩衝區,而必需執行它之後,必須作為額外的參數提供緩衝區大小,以字元為單位。如需詳細資訊,請參閱 scanf_s、 _scanf_s_l、 wscanf_s、 _wscanf_s_lscanf 型別功能變數字元

注意事項注意事項

大小參數屬於型別unsigned, size_t。

format解譯型別的欄位,並具有相同的引數控制項形成和運作format引數的scanf_s函式。如果複製之間所經歷重疊的字串,這個行為未定義。

swscanf_s寬字元版本的sscanf_s;引數去swscanf_s是寬字元字串。sscanf_s無法處理多位元組的十六進位字元。swscanf_s無法處理 Unicode 十六進位的全形或 「 相容性區域"字元。否則, swscanf_s和sscanf_s運作方式完全相同。

使用這些函式的版本_l尾碼完全相同,不同之處在於它們使用傳遞中而不是目前執行緒的地區設定的地區設定參數。

泛用文字常式對應

TCHAR。H 常式

_UNICODE & 未定義的 _MBCS

定義的 _MBCS

定義 _unicode 之後

_stscanf_s

sscanf_s

sscanf_s

swscanf_s

_stscanf_s_l

_sscanf_s_l

_sscanf_s_l

_swscanf_s_l

需求

常式

所需的標頭

sscanf_s, _sscanf_s_l

<stdio.h>

swscanf_s, _swscanf_s_l

<stdio.h> 或者 <wchar.h>

其他的相容性資訊,請參閱相容性在簡介中。

範例

// crt_sscanf_s.c
// This program uses sscanf_s to read data items
// from a string named tokenstring, then displays them.

#include <stdio.h>
#include <stdlib.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 plus NULL terminator
   sscanf_s( tokenstring, "%s", s, _countof(s) );
   sscanf_s( tokenstring, "%c", &c, sizeof(char) );
   sscanf_s( tokenstring, "%d", &i );
   sscanf_s( tokenstring, "%f", &fp );

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

.NET Framework 對等用法

請參閱Parse方法,例如 System::Double::Parse

請參閱

參考

資料流 I/O

fscanf、 _fscanf_l、 fwscanf、 _fwscanf_l

scanf、 _scanf_l、 wscanf、 _wscanf_l

sprintf、 _sprintf_l、 swprintf、 _swprintf_l、 __swprintf_l

_snprintf、 _snprintf_l、 _snwprintf、 _snwprintf_l