2014-07-24 4 views
4

wchar_t *를 파일에 쓰려고 합니다만 컴파일 된 프로그램의 명령 행 출력은 다음과 같습니다. 본질적으로 그리스어 문자열을 쓰려고 할 때 프로그램이 멈 춥니 다. wofstream에 쓰면 예외가 발생합니다.

#include <iostream> 
#include <stdio.h> 
#include <fstream> 
#include <wchar.h> 
using namespace std; 

int main(int argc, char **argv) 
{ 

    printf("%s\n",setlocale(LC_ALL,"")); 
    wofstream f("xxx.txt", ios::out); 
    if(f.is_open()) 
    { 
     try 
     { 
      f.write(L"good",4); 
      f.flush(); 
      f.write(L"καλημερα",8); 
      f.close(); 
     } 
     catch (int e) 
     { 
      cout << "An exception occurred. Exception Nr. " << e <<endl; 
     } 

     printf("hello world\n"); 
     return 0; 
    } 
} 

아래

el_GR.UTF-8 
terminate called after throwing an instance of 'int' 
Ακυρώθηκε (core dumped) 

소스 코드?

+0

그것은 버그입니다. 컴파일러와 전체 시스템은 무엇입니까? 경고가 있습니까? 소스 코드의 인코딩은 무엇입니까? 그리고 마지막으로'e'의 가치는 무엇입니까? –

+0

e의 값은 20입니다. g ++을 사용하고 경고없이 컴파일합니다. 그것은 일종의 초기화가 수행되어야하는 것처럼 보입니다. –

답변

1

스트림은 환경에서 로케일을 사용하지 않습니다. 나는 addeed :

#include <locale> 
locale loc("el_GR.utf8"); 
f.imbue(loc); 

그래서 스트림이 지금 (내가 수정하시기 바랍니다 틀렸다 경우)를 사용하는 로케일을 보유하고 있습니다. 제대로 작동

코드는 다음 IOSTREAMS 아무것도 이제까지 int를 넣지해야으로

#include <iostream> 
#include <stdio.h> 
#include <fstream> 
#include <wchar.h> 
#include <locale> 


using namespace std; 

int main(int argc, char **argv) 
{ 
    locale loc("el_GR.utf8"); 
    wcout<<"Hi I am starting Ξεκινάμε"<<endl; 
    cout<<"%s\n"<<setlocale(LC_ALL,"en_US.utf8")<<endl; 
    wofstream f("xxx.txt", ios::out); 
     if(f.is_open()){ 
      f.imbue(loc); 

      f.write(L"good",4);f.flush(); 
      f.write(L"καλημέρα",8); 
      f.close(); 
      cout<<"fileClosed"<<endl; 
    } 

    cout<<"hello world"<<endl; 
    return 0; 


} 
+0

'getenv'를 사용하여 환경에서 로케일을 가져올 수 있습니다. – Brian

+1

또는 관련로운 envvars를 모두 고려하기 위해서'locale loc (setlocale (LC_ALL, ""))'을 사용하십시오 ... –

+0

f.imbue (locale (setlocale (LC_ALL, "")))); 작동하고 –

관련 문제