Redigera

Dela via


_cscanf, _cscanf_l, _cwscanf, _cwscanf_l

Reads formatted data from the console. More secure versions of these functions are available; see _cscanf_s, _cscanf_s_l, _cwscanf_s, _cwscanf_s_l.

Note

In Visual Studio 2015 The printf and scanf family of functions were declared as inline and moved to the <stdio.h> and <conio.h> headers. If you are migrating older code you might see Linker Error LNK2019 in connection with these functions. For more information, see Visual C++ change history 2003 - 2015.

Important

This API cannot be used in applications that execute in the Windows Runtime. For more information, see CRT functions not supported in Universal Windows Platform apps.

Syntax

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] ...
);

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 doesn't include fields that were read but not assigned. The return value is EOF for an attempt to read at end of file. An EOF can also be returned when keyboard input is redirected at the operating-system command-line level. A return value of zero means that no fields were assigned.

Remarks

The _cscanf 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 function. While _cscanf normally echoes the input character, it doesn't do so if the last call was to _ungetch.

This function validates its parameters. If format is NULL, the invalid parameter handler is invoked, as described in Parameter validation. If execution is allowed to continue, errno is set to EINVAL and the function returns EOF.

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 _cscanf _cscanf _cwscanf
_tcscanf_l _cscanf_l _cscanf_l _cwscanf_l

Requirements

Routine Required header
_cscanf, _cscanf_l <conio.h>
_cwscanf, _cwscanf_l <conio.h> or <wchar.h>

For more compatibility information, see Compatibility.

Example

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

See also

Console and port 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