_ungetch
、 、 _ungetwch
、 _ungetch_nolock
_ungetwch_nolock
推送回讀取自主控台的最後一個字元。
重要
這個 API 不能用於在 Windows 執行階段中執行的應用程式。 如需詳細資訊,請參閱 CRT functions not supported in Universal Windows Platform apps (通用 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
由 或 _getche
(或 _getwch
) _getwche
讀取_getch
下一個字元。 _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 |
需求
常式 | 必要的標頭 |
---|---|
_ungetch , _ungetch_nolock |
<conio.h> |
_ungetwch , _ungetwch_nolock |
<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
_cscanf
、 、 _cscanf_l
、 _cwscanf
_cwscanf_l
_getch
, _getwch