Table of Contents
Visual C++ – 도스창에서 한글깨짐 해결
소스코드는 UTF-8 이어야 하고,
컴파일 명령은 아래와 같다.
cl /MT /O2 /utf-8 /W4 /EHsc test.c /link advapi32.lib user32.lib /OUT:test.exe
#include <stdio.h>
#include <locale.h>
#include <wchar.h>
#include <string.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <langinfo.h>
#endif
char* getSystemLocale() {
static char locale[100];
#ifdef _WIN32
LCID lcid = GetSystemDefaultLCID();
GetLocaleInfoA(lcid, LOCALE_SISO639LANGNAME, locale, sizeof(locale));
#else
char* sysLocale = setlocale(LC_ALL, "");
if (sysLocale) {
strncpy(locale, sysLocale, sizeof(locale) - 1);
locale[sizeof(locale) - 1] = '\0';
}
#endif
return locale;
}
int main(void) {
char* sysLocale = getSystemLocale();
setlocale(LC_ALL, sysLocale);
wchar_t text[100];
wprintf(L"텍스트 입력: ");
fgetws(text, 100, stdin);
wprintf(L"입력값: %ls\n", text);
return 0;
}