getch,getche,getc,getwc,getchar,getwchar

IN MSDN ——>

_getch, _getche 

Get a character from the console without echo (_getch) or with echo (_getche).

int _getch( void );

int _getche( void );

RoutineRequired HeaderCompatibility
_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.LIBSingle thread static library, retail version
LIBCMT.LIBMultithread static library, retail version
MSVCRT.LIBImport 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

____________________________________________________________________________________________________________
———— ——————————————— ———————————————————————————————————————————

从控制台获取一个字符
getch   :无回显   
getche  :有回显


Compatibility

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. 

兼容性
微软的运行时库支持美国 美国国家标准协会(ANSI)C和UNIXC。在这本书中,引用 UNIX包括XENIX,其他类似UNIX系统,和在Windows NT和Windows 95中的POSIX子系统。这本书在描述每个运行时库例程时包含了对这些目标的一个兼容性部分:ANSI,Windows 95(列如 win 95),和Windows NT(Win NT)。这种产品随附的所有运行时库例程都兼容 Win32 API。

附记

_getch函数从控制台读取一个字符,但不回显。_getche函数从控制台读取一个字符,并其回显该字符。它们都不能用来读取CTRL + C。当读取一个功能键或者一个箭头键,_getch和_getche必须被调用两次;第一次调用返回0或0 xe0,第二次调用返回实际的关键代码。


____________________________________________________________________________________________________________
———— ——————————————— ———————————————————————————————————————————

getc, getwc, getchar, getwchar

Read a character from a stream (getcgetwc), or get a character from stdin (getchargetwchar).

int getc( FILE *stream );

wint_t getwc( FILE *stream );

int getchar( void );

wint_t getwchar( void );

RoutineRequired HeaderCompatibility
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.LIBSingle thread static library, retail version
LIBCMT.LIBMultithread static library, retail version
MSVCRT.LIBImport 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.

RoutineRemarks
getcSame as fgetc, but implemented as a function and as a macro.
getwcWide-character version of getc. Reads a multibyte character or a wide character according to whether stream is opened in text mode or binary mode.
getcharSame as _fgetchar, but implemented as a function and as a macro.
getwcharWide-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
_gettcgetcgetcgetwc
_gettchargetchargetchargetwchar

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), 或从标准输入缓存(stdin)中得到一个字符(getchar, getwchar)这些函数返回所读取的字符。为了表明一个读取错误或文件结束的条件,getc和getchar函数返回EOF,getwc和getwchar返回WEOF。对于getc和getchar,使用ferror或feof去检查是否有错误或文件结束

每个例程从一个文件的当前位置读取一个字符,增加相关文件的指针(如果定义了),使其指向下一个字符。对于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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值