Created
January 28, 2015 12:23
-
-
Save ynkdir/d97d276ab9fa162c914e to your computer and use it in GitHub Desktop.
windows unicode stdout
This file contains 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
// a.exe => locale encoding | |
// a.exe | cmd => utf-8 | |
// a.exe > file => utf-16le | |
#include <stdio.h> | |
#include <fcntl.h> | |
#include <io.h> | |
#include <locale.h> | |
#include <sys/stat.h> | |
#include <sys/types.h> | |
int isfifo(int fd) | |
{ | |
struct stat st; | |
if (fstat(fd, &st) != 0) | |
return 0; | |
return st.st_mode & _S_IFIFO; | |
} | |
int isfile(int fd) | |
{ | |
struct stat st; | |
if (fstat(fd, &st) != 0) | |
return 0; | |
return st.st_mode & _S_IFREG; | |
} | |
int main() | |
{ | |
setlocale(LC_ALL, ""); | |
if (_isatty(_fileno(stdout))) { | |
// terminal | |
} else if (isfifo(_fileno(stdout))) { | |
// pipe | |
_setmode(_fileno(stdout), _O_U8TEXT); | |
wprintf(L"\xFEFF"); // BOM | |
} else if (isfile(_fileno(stdout))) { | |
// file | |
_setmode(_fileno(stdout), _O_U16TEXT); | |
wprintf(L"\xFEFF"); // BOM | |
} | |
wprintf(L"こんにちは世界\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment