_cscanf, _cscanf_l, _cwscanf, _cwscanf_l

從主控台讀取格式化資料。 這些函式有更安全的版本可供使用;請參閱 、、 _cwscanf_s_cscanf_s_l_cwscanf_s_l_cscanf_s

注意

在 Visual Studio 2015 中,函 printf 式的 和 scanf 系列已宣告為 inline ,並移至 <stdio.h><conio.h> 標頭。 如果您要移轉較舊的程式碼,您可能會看到連結器錯誤LNK2019與這些函式連線。 如需詳細資訊,請參閱 Visual C++ 變更歷程記錄 2003 - 2015

重要

這個 API 不能用於在 Windows 執行階段中執行的應用程式。 如需詳細資訊,請參閱 CRT functions not supported in Universal Windows Platform apps (通用 Windows 平台應用程式中不支援的 CRT 函式)。

語法

int _cscanf(
   const char *format [,
   argument] ...
);
int _cscanf_l(
   const char *format,
   _locale_t locale [,
   argument] ...
);
int _cwscanf(
   const wchar_t *format [,
   argument] ...
);
int _cwscanf_l(
   const wchar_t *format,
   _locale_t locale [,
   argument] ...
);

參數

format
格式控制字串。

argument
選用參數。

locale
要使用的地區設定。

傳回值

已成功轉換並指派的欄位數目。 傳回值不包含已讀取但未指派的欄位。 若嘗試讀取檔案結尾,則傳回值為 EOFEOF當鍵盤輸入在作業系統命令列層級重新導向時,也可以傳回 。 零的傳回值表示未指派任何欄位。

備註

_cscanf 函式會將資料直接從主控台讀入 argument 指定的位置。 函 _getche 式是用來讀取字元。 每個選擇性參數都必須是其類型對應至 format 中類型規範的變數指標。 格式會控制輸入欄位的解譯,而且其形式和函式與 format 函式的參數 scanf 相同。 雖然 _cscanf 通常會回應輸入字元,但如果最後一個呼叫是 , _ungetch 則不會這麼做。

這個函式會驗證它的參數。 如果 format 為 NULL ,則會叫用不正確參數處理常式,如參數驗證 中所述 。 若允許繼續執行, errno 會設為 EINVAL ,且函式會傳回 EOF中所述。

這些有 _l 尾碼的函式版本是一樣的,不同之處在於會使用傳入的地區設定,而不使用目前的執行緒地區設定。

泛型文字常式對應

TCHAR.H 常式 _UNICODE_MBCS 未定義 _MBCS 定義 _UNICODE 定義
_tcscanf _cscanf _cscanf _cwscanf
_tcscanf_l _cscanf_l _cscanf_l _cwscanf_l

需求

常式 必要的標頭
_cscanf, _cscanf_l <conio.h>
_cwscanf, _cwscanf_l <conio.h > 或 < wchar.h>

如需相容性詳細資訊,請參閱相容性

範例

// crt_cscanf.c
// compile with: /c /W3
/* This program prompts for a string
* and uses _cscanf to read in the response.
* Then _cscanf returns the number of items
* matched, and the program displays that number.
*/

#include <stdio.h>
#include <conio.h>

int main( void )
{
   int   result, i[3];

   _cprintf_s( "Enter three integers: ");
   result = _cscanf( "%i %i %i", &i[0], &i[1], &i[2] ); // C4996
   // Note: _cscanf is deprecated; consider using _cscanf_s instead
   _cprintf_s( "\r\nYou entered " );
   while( result-- )
      _cprintf_s( "%i ", i[result] );
   _cprintf_s( "\r\n" );
}
1 2 3
Enter three integers: 1 2 3
You entered 3 2 1

另請參閱

主控台和埠 I/O
_cprintf, _cprintf_l, _cwprintf, _cwprintf_l
fscanf, _fscanf_l, fwscanf, _fwscanf_l
scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l
sscanf, _sscanf_l, swscanf, _swscanf_l