_cscanf_s, _cscanf_s_l, _cwscanf_s, _cwscanf_s_l
Read formatted data from the console. These are more secure versions of _cscanf, _cscanf_l, _cwscanf, _cwscanf_l with security enhancements as described in Security Enhancements in the CRT.
int _cscanf_s(
const char *format [,
argument] ...
);
int _cscanf_s_l(
const char *format,
locale_t locale [,
argument] ...
);
int _cwscanf_s(
const wchar_t *format [,
argument] ...
);
int _cwscanf_s_l(
const wchar_t *format,
locale_t locale [,
argument] ...
);
Parameters
format
Format-control string.argument
Optional parameters.locale
The locale to use.
Return Value
The number of fields that were successfully converted and assigned. The return value does not include fields that were read but not assigned. The return value is EOF for an attempt to read at end of file. This can occur when keyboard input is redirected at the operating-system command-line level. A return value of 0 means that no fields were assigned.
These functions validate their parameters. If format is a null pointer, these functions invoke the invalid parameter handler, as described in Parameter Validation. If execution is allowed to continue, these functions return EOF and errnois set to EINVAL.
Remarks
The _cscanf_s function reads data directly from the console into the locations given by argument. The _getche function is used to read characters. Each optional parameter must be a pointer to a variable with a type that corresponds to a type specifier in format. The format controls the interpretation of the input fields and has the same form and function as the format parameter for the scanf_s function. While _cscanf_s normally echoes the input character, it does not do so if the last call was to _ungetch.
Like other secure versions of functions in the scanf family,_cscanf_s and _cswscanf_s require size arguments for the type field characters c, C, s, S, and [. For more information, see scanf Width Specification.
Note
The size parameter is of type unsigned, not size_t.
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 routine |
_UNICODE and _MBCS not defined |
_MBCS defined |
_UNICODE defined |
---|---|---|---|
_tcscanf_s |
_cscanf_s |
_cscanf_s |
_cwscanf_s |
_tcscanf_s_l |
_cscanf_s_l |
_cscanf_s_l |
_cwscanf_s_l |
Requirements
Routine |
Required header |
---|---|
_cscanf_s, _cscanf_s_l |
<conio.h> |
_cwscanf_s, _cwscanf_s_l |
<conio.h> or <wchar.h> |
For more compatibility information, see Compatibility in the Introduction.
Libraries
All versions of the C run-time libraries.
Example
// crt_cscanf_s.c
// compile with: /c
/* This program prompts for a string
* and uses _cscanf_s to read in the response.
* Then _cscanf_s returns the number of items
* matched, and the program displays that number.
*/
#include <stdio.h>
#include <conio.h>
int main( void )
{
int result, n[3];
int i;
result = _cscanf_s( "%i %i %i", &n[0], &n[1], &n[2] );
_cprintf_s( "\r\nYou entered " );
for( i=0; i<result; i++ )
_cprintf_s( "%i ", n[i] );
_cprintf_s( "\r\n" );
}
Input
1 2 3
Output
You entered 1 2 3
See Also
Reference
_cprintf, _cprintf_l, _cwprintf, _cwprintf_l
fscanf_s, _fscanf_s_l, fwscanf_s, _fwscanf_s_l