Share via


_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 ,並 _ungetwchWEOF 回 。

備註

這些函式會將字元 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