简体   繁体   中英

FillConsoleOutputCharacter/WriteConsoleOutput and special characters

I'm messing with some of the native windows console functions, and am impressed by their speed,if not their ease of use.

Anyway, I have long known that the following code will produce some interesting characters

for(int i = 0; i < 256; i++)
{
    cout << char(i) << endl;
}

However, I cannot get FillConsoleOutputCharacter or WriteConsoleOutput to produce all of those characters (many simply appear as question marks).

Here is an example of the code I am using:

COORD spot = {0,0};
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD Written;

for(int i = 0; i < 256; i++)
{
    FillConsoleOutputAttribute(hOut, 7, 1, spot, &Written);
    FillConsoleOutputCharacterW(hOut, char(i), 1, spot, &Written);

    spot.Y++;
}

Does anyone know of a relatively convenient way to write those characters with the native functions?

By the way, I am using Visual Studio 2010 on Windows 7 x64.

Try using FillConsoleOutputCharacterA instead of FillConsoleOutputCharacterW which is using the unicode character which can take a little bit of knowledge to get correctly.

edit I tried using FillConsoleOutputCharacterA and it gives equivalent output to your first case.

FillConsoleOutputCharacterA should write the same set of characters that the cout function does. These characters are determined by the console's current code page.

With FillConsoleOutputCharacterW, you can still generate all the same characters (as well as any additional characters that may be included in the console font) but you need to use the Unicode (16-bit) codes for these characters, rather than the 8-bit codes used with cout.

Note that Windows internally uses an out-of-date version of Unicode, with characters limited to 16 bits (0-65536) rather than Unicode proper which uses 0-1,112,063 (although most of these codes remain unassigned). I believe the console's Unicode character set corresponds to plane 0 of Unicode, the basic multilingual plane.

The question marks appear when you write a control character or a character that isn't included in the current font.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM