推送回未从控制台读取的最后一个字符。
重要
此 API 不能用于在 Windows 运行时中执行的应用程序。 有关详细信息,请参阅通用 Windows 平台应用中不支持的 CRT 函数。
语法
int _ungetch(
int c
);
wint_t _ungetwch(
wint_t c
);
int _ungetch_nolock(
int c
);
wint_t _ungetwch_nolock(
wint_t c
);
参数
c
要推送的字符。
返回值
如果成功则两个函数均返回字符 c
。 如有错误,_ungetch
将返回 EOF
的值,_ungetwch
返回 WEOF
。
备注
这些函数将字符 c
推送回控制台,从而使 c
成为 _getch
或 _getche
(或者 _getwch
或 _getwche
)读取的下一个字符。 _ungetch
和 _ungetwch
在下一次读取前被调用了 一次以上将会失败。 c
参数可以不是 EOF
(或 WEOF
)。
带 _nolock
后缀的版本相同,但可能受到其他线程的影响。 它们可能更快,因为不会产生锁定其他线程的开销。 仅在线程安全的上下文中使用这些函数,如单线程应用程序或调用范围已经处理线程隔离。
默认情况下,此函数的全局状态范围限定为应用程序。 若要更改此行为,请参阅 CRT 中的全局状态。
一般文本例程映射
TCHAR.H 例程 | _UNICODE 和 _MBCS 未定义 |
_MBCS 已定义 |
_UNICODE 已定义 |
---|---|---|---|
_ungettch |
_ungetch |
_ungetch |
_ungetwch |
_ungettch_nolock |
_ungetch_nolock |
_ungetch_nolock |
_ungetwch_nolock |
要求
例程 | 必需的标头 |
---|---|
%> | <conio.h> |
%> | <conio.h> 或 <wchar.h> |
有关兼容性的详细信息,请参阅 兼容性。
示例
// crt_ungetch.c
// compile with: /c
// In this program, a white-space delimited
// token is read from the keyboard. When the program
// encounters a delimiter, it uses _ungetch to replace
// the character in the keyboard buffer.
//
#include <conio.h>
#include <ctype.h>
#include <stdio.h>
int main( void )
{
char buffer[100];
int count = 0;
int ch;
ch = _getche();
while( isspace( ch ) ) // Skip preceding white space.
ch = _getche();
while( count < 99 ) // Gather token.
{
if( isspace( ch ) ) // End of token.
break;
buffer[count++] = (char)ch;
ch = _getche();
}
_ungetch( ch ); // Put back delimiter.
buffer[count] = '\0'; // Null terminate the token.
printf( "\ntoken = %s\n", buffer );
}
Whitetoken = White
另请参阅
控制台和端口 I/O
%>