For character-based applications that only want to do simple reading from Standard Input and writing to Standard Output, the console is accessible with the File functions. This means that you use ReadFile with a handle to StdIn to get keyboard input or to read input from a file or a pipe if StdIn has been redirected. To write to the console window, you use WriteFile with a handle to StdOut or StdErr.
Using ReadFile with a handle to StdIn, the application can receive input from the keyboard. ReadFile only returns keyboard events that can be translated into ASCII characters, including control key combinations. The application does not receive keyboard events involving the function keys or direction keys; it has no access to mouse input; and it is not aware of window resize events.
If line input is enabled (the default), ReadFile does not return to the caller until the Enter key is pressed. In character mode (line input disabled), ReadFile does not return until at least one key is available. In either input mode, ReadFile transfers all available characters to the caller until either no more keys are available, or the caller's buffer is filled. In this case, the remaining characters are buffered until the next call to ReadFile . The total number of bytes actually read is returned to the caller.
Using WriteFile with a handle to StdOut or StdErr, the application can write to the console window. Characters are transferred from a buffer and written to the active screen buffer at the current cursor location, advancing the cursor to the next position.
Output for applications using the File functions is affected by the echo mode of the input handle, and by the processed output and wrap at EOL modes of the output handle. Refer to the section above on console modes for a discussion of the effects of these modes.
Use the SetConsoleTextAttribute call to set the foreground and background colors with which subsequent characters will be written to the screen buffer. This call affects only characters written by WriteFile or echoed by ReadFile. Characters written by any of the Console functions either explicitly specify their attributes or leave the attributes at the positions written unchanged. The current text attributes may be determined by calling GetConsoleScreenBufferInfo.
The following code fragment uses the File functions for console input and output:
#define FOREGROUND_WHITE \
(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
CHAR prompt1[] = "Type something and press Enter:\\n.";
CHAR prompt2[] = "Type any key: ";
CHAR Buffer[100];
DWORD dwBytesRead, dwBytesWritten, dwMode;
BOOL bSuccess;
HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE);
HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
if (!hInput || !hOutput) {
/* NULL handle, process error */
}
// draw white characters on a red background
bSuccess = SetConsoleTextAttribute(hOutput,
FOREGROUND_WHITE | BACKGROUND_RED);
if (!bSuccess) {
/* process error */
}
// default input modes: line, processed, echo
// default output modes: processed, wrap at EOL
// prompt user to enter a line
bSuccess = WriteFile(hOutput, prompt1, strlen(prompt1),
&dwBytesWritten, NULL);
if (!bSuccess) {
/* process error */
}
// wait for input which is echoed to the screen
bSuccess = ReadFile(hInput, Buffer, 100, &dwBytesRead, NULL);
if (!bSuccess) {
/* process error */
}
// get the console mode, so we can change it
bSuccess = GetConsoleMode(hInput, &dwMode);
if (!bSuccess) {
/* process error */
}
// turn off line input and echo
dwMode &= ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT);
bSuccess = SetConsoleMode(hInput, dwMode);
if (!bSuccess) {
/* process error */
}
// prompt user to type any key
bSuccess = WriteFile(hOutput, prompt2, strlen(prompt2),
&dwBytesWritten, NULL);
if (!bSuccess) {
/* process error */
}
// wait for input which is not echoed to the screen
bSuccess = ReadFile(hInput, Buffer, 100, &dwBytesRead, NULL);
// write input and carriage return to the screen
Buffer[dwBytesRead] = '\\n';
bSuccess = WriteFile(hOutput, Buffer, dwBytesRead+1,
&dwBytesWritten, NULL);
if (!bSuccess) {
/* process error */
}