Console and Port I/O

The console and port I/O routines are implemented as functions and are declared in the include file CONIO.H. These functions perform reading and writing operations on your console or on the specified port. The _cgets, _cscanf, _getch, _getche, and _kbhit routines take input from the console, while _cprintf, _cputs, _putch, and _ungetch write to the console. The input or output of these functions can be redirected.

Routine Use

_cgets Reads a string from the console
_cprintf Writes formatted data to the console
_cputs Writes a string to the console
_cscanf Reads formatted data from the console
_getch Reads a character from the console
_getche Reads a character from the console and echoes it
_inp Reads one byte from the specified I/O port
_inpw Reads a two-byte word from the specified I/O port
_kbhit Checks for a keystroke at the console
_outp Writes one byte to the specified I/O port
_outpw Writes a two-byte word to the specified I/O port
_putch Writes a character to the console
_ungetch “Ungets” the last character read from the console so that it becomes the next character read

Note:

Programs that need only run under DOS can also use a number of direct DOS I/O system calls (_dos_open, _dos_read, _dos_close, etc.). These are described in detail in “System Calls”.

The console or port does not have to be opened or closed before I/O is performed, so there are no open or close routines in this category. The port I/O routines _inp and _outp read or write one byte at a time from the specified port. The _inpw and _outpw routines read and write two-byte words, respectively.

The console I/O routines allow reading and writing of strings (_cgets and _cputs), formatted data (_cscanf and _cprintf), and characters. Several options are available when reading and writing characters.

The _putch routine writes a single character to the console. The _getch and _getche routines read a single character from the console: _getche echoes the character back to the console, while _getch does not. The _ungetch routine “ungets” the last character read; the next read operation on the console begins with the “ungotten” character.

The _kbhit routine determines whether a key has been struck at the console. This routine allows you to test for keyboard input before you attempt to read from the console.

Note:

The console I/O routines are not compatible with stream or low-level library routines and should not be used with them.