_getch, _getche
Get a character from the console without echo (_getch) or with echo (_getche).
int _getch( void );
int _getche( void );
Routine | Required Header | Compatibility |
_getch | <conio.h> | Win 95, Win NT |
_getche | <conio.h> | Win 95, Win NT |
For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIB | Single thread static library, retail version |
LIBCMT.LIB | Multithread static library, retail version |
MSVCRT.LIB | Import library for MSVCRT.DLL, retail version |
Return Value
Both _getch and _getche return the character read. There is no error return.
Remarks
The _getch function reads a single character from the console without echoing. _getche reads a single character from the console and echoes the character read. Neither function can be used to read CTRL+C. When reading a function key or an arrow key, _getch and _getche must be called twice; the first call returns 0 or 0xE0, and the second call returns the actual key code.
Example
/* GETCH.C: This program reads characters from
* the keyboard until it receives a 'Y' or 'y'.
*/
#include <conio.h>
#include <ctype.h>
void main( void )
{
int ch;
_cputs( "Type 'Y' when finished typing keys: " );
do
{
ch = _getch();
ch = toupper( ch );
} while( ch != 'Y' );
_putch( ch );
_putch( '\r' ); /* Carriage return */
_putch( '\n' ); /* Line feed */
}
Output
Type 'Y' when finished typing keys: Y
getche :有回显
The Microsoft run-time library supports American National Standards Institute (ANSI) C and UNIX C. In this book, references to UNIX include XENIX, other UNIX-like systems, and the POSIX subsystem in Windows NT and Windows 95. The description of each run-time library routine in this book includes a compatibility section for these targets: ANSI, Windows 95 (listed as Win 95), and Windows NT (Win NT). All run-time library routines included with this product are compatible with the Win32 API.
附记
_getch函数从控制台读取一个字符,但不回显。_getche函数从控制台读取一个字符,并其回显该字符。它们都不能用来读取CTRL + C。当读取一个功能键或者一个箭头键,_getch和_getche必须被调用两次;第一次调用返回0或0 xe0,第二次调用返回实际的关键代码。
getc, getwc, getchar, getwchar
Read a character from a stream (getc, getwc), or get a character from stdin (getchar, getwchar).
int getc( FILE *stream );
wint_t getwc( FILE *stream );
int getchar( void );
wint_t getwchar( void );
Routine | Required Header | Compatibility |
getc | <stdio.h> | ANSI, Win 95, Win NT |
getwc | <stdio.h> or <wchar.h> | ANSI, Win 95, Win NT |
getchar | <stdio.h> | ANSI, Win 95, Win NT |
getwchar | <stdio.h> or <wchar.h> | ANSI, Win 95, Win NT |
For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIB | Single thread static library, retail version |
LIBCMT.LIB | Multithread static library, retail version |
MSVCRT.LIB | Import library for MSVCRT.DLL, retail version |
Return Value
Each of these functions returns the character read. To indicate an read error or end-of-file condition, getc and getchar return EOF, and getwc and getwchar return WEOF. For getc and getchar, use ferror or feof to check for an error or for end of file.
Parameter
stream
Input stream
Remarks
Each of these routines reads a single character from a file at the current position and increments the associated file pointer (if defined) to point to the next character. In the case of getc and getwc, the file is associated with stream (see Choosing Between Functions and Macros). Routine-specific remarks follow.
Routine | Remarks |
getc | Same as fgetc, but implemented as a function and as a macro. |
getwc | Wide-character version of getc. Reads a multibyte character or a wide character according to whether stream is opened in text mode or binary mode. |
getchar | Same as _fgetchar, but implemented as a function and as a macro. |
getwchar | Wide-character version of getchar. Reads a multibyte character or a wide character according to whether stream is opened in text mode or binary mode. |
Generic-Text Routine Mappings
TCHAR.H Routine | _UNICODE & _MBCS Not Defined | _MBCS Defined | _UNICODE Defined |
_gettc | getc | getc | getwc |
_gettchar | getchar | getchar | getwchar |
Example
/* GETC.C: This program uses getchar to read a single line
* of input from stdin, places this input in buffer, then
* terminates the string before printing it to the screen.
*/
#include <stdio.h>
void main( void )
{
char buffer[81];
int i, ch;
printf( "Enter a line: " );
/* Read in single line from "stdin": */
for( i = 0; (i < 80) && ((ch = getchar()) != EOF) && (ch != '\n'); i++ )
buffer[i] = (char)ch;
/* Terminate string with null character: */
buffer[i] = '\0';
printf( "%s\n", buffer );
}
Output
Enter a line: This is a testThis is a test
每个例程从一个文件的当前位置读取一个字符,增加相关文件的指针(如果定义了),使其指向下一个字符。对于getc和getwc,文件与流相关(请参阅函数和宏之间的选择)。与例程相关的常规解释如下:
getc 类似fgetc,但作为函数和宏实现
getwc 宽字符版本的getc。根据流是否在文本模式或二进制模式中打开,读取一个多字节字符或宽字符
getchar 类似fgetchar,但作为函数和宏实现
getwchar 宽字符版本的getchar。根据流是否在文本模式或二进制模式中打开,读取一个多字节字符或宽字符
#include<stdio.h>
#include<conio.h>
int main(int argc, char* argv[])
{
int str[7];
printf("Please input six character: \n");
for(int i = 0; i < 6; i++) //input six character
{
str[i] = getch(); //In the form of ASCII stored
printf("*");
}
printf("\n");
for(int j = 0; j < 6; j++)
{
printf("%c", str[j]); //so output by "%c"
}
printf("\n");
return 0;
}