Created
January 21, 2017 16:23
-
-
Save minoki/63a04c9f49363bd8c4e91c9983a89baa to your computer and use it in GitHub Desktop.
Test programs for Windows Console
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <locale.h> | |
#include <io.h> | |
#include <fcntl.h> | |
#include <windows.h> | |
BOOL is_stream_console(FILE *stream) | |
{ | |
DWORD mode; | |
// GetConsoleMode requires GENERIC_READ access on the handle | |
return !!GetConsoleMode((HANDLE)_get_osfhandle(_fileno(stream)), &mode); | |
} | |
BOOL is_stream_console_output(FILE *stream) | |
{ | |
DWORD written; | |
return !!WriteConsoleW((HANDLE)_get_osfhandle(_fileno(stream)), NULL, 0, &written, NULL); | |
} | |
void print_stream_info(const char *name, FILE *stream) | |
{ | |
fprintf(stderr, "%s is %sa tty.\n", name, _isatty(_fileno(stdout)) ? "" : "not "); | |
fprintf(stderr, "%s is %sa console.\n", name, is_stream_console(stdout) ? "" : "not "); | |
fprintf(stderr, "%s is %sa console output.\n", name, is_stream_console_output(stdout) ? "" : "not "); | |
} | |
int main(int argc, char *argv[]) | |
{ | |
_setmode(_fileno(stdout), _O_U8TEXT); | |
print_stream_info("stdout", stdout); | |
fputws(L"Caf\u00e9 au lait\n", stdout); | |
fputws(L"\u732B\n", stdout); | |
const char *names[] = {"CON", "CONOUT$"}; | |
const char *modes[] = {"w", "w+"}; | |
for (int i = 0; i < 2; ++i) { | |
for (int j = 0; j < 2; ++j) { | |
const char *name = names[i]; | |
const char *mode = modes[j]; | |
FILE *f = fopen(name, mode); | |
if (f == NULL) { | |
fprintf(stderr, "fopen(%s, %s) failed.\n", name, mode); | |
} else { | |
_setmode(_fileno(f), _O_U8TEXT); | |
char stream_name[100]; | |
sprintf(stream_name, "fopen(%s, %s)", name, mode); | |
print_stream_info(stream_name, f); | |
fputws(L"Caf\u00e9 au lait\n", f); | |
fputws(L"\u732B\n", f); | |
fclose(f); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <io.h> | |
#include <fcntl.h> | |
int main(int argc, char *argv[]) | |
{ | |
_setmode(_fileno(stdout), _O_U8TEXT); | |
fputws(L"Caf\u00e9 au lait\n", stdout); | |
fputws(L"\u732B\n", stdout); | |
//puts("Hello world!"); // puts() fails because stdout is Unicode mode. | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <io.h> | |
#include <fcntl.h> | |
#include <windows.h> | |
int main(int argc, char *argv[]) | |
{ | |
_setmode(_fileno(stdout), _O_BINARY); | |
fprintf(stdout, "Console output code page is %d. (should be 65001)\n", GetConsoleOutputCP()); | |
fputs(u8"Caf\u00e9 au lait\n", stdout); | |
fputs(u8"\u732B\n", stdout); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <locale.h> | |
#include <io.h> | |
#include <fcntl.h> | |
#include <windows.h> | |
int main(int argc, char *argv[]) | |
{ | |
setlocale(LC_CTYPE, "C"); // LC_CTYPE must be "C" | |
_setmode(_fileno(stdout), _O_TEXT); | |
fprintf(stdout, "Console output code page is %d. (should be 65001)\n", GetConsoleOutputCP()); | |
fputs(u8"Caf\u00e9 au lait\n", stdout); | |
fputs(u8"\u732B\n", stdout); | |
setvbuf(stdout, NULL, _IOFBF, 1024); | |
fputws(L"Caf\xc3\xa9 au lait\n", stdout); | |
fputws(L"\xe7\x8c\xab\n", stdout); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment