0

I use mingw compiler. How to convert wchar_t to char ?

Data(HWND hwnd,  wchar_t szFileName[MAX_PATH])
{
    string sIn;
    ifstream infile;
    infile.open(szFileName);
    infile.seekg(0,ios::beg);

    // fill vector with file rows
    while ( getline(infile,sIn ) )
    {
       fileRows.push_back(sIn);
    }
}

I would like to convert wchar_t szFileName to char szFileNameChar.

Carl Mark
  • 371
  • 1
  • 12
  • 23
  • [`std::wstring_convert`](http://en.cppreference.com/w/cpp/locale/wstring_convert) should help. – chris Apr 02 '13 at 02:03
  • 1
    Bad question. char and wchar don't describe the encoding of what is stored. What you need to ask is what is the encoding of the text in your strings (both char and wchar_t versions). Once you have this then we can discuss how to convert from one to the other. – Martin York Apr 02 '13 at 02:30

1 Answers1

5
string str("i'm char[]");
wstring wstr(str.begin(), str.end());

wstring wstr(L"i'm wchar_t[]");
string str(wstr.begin(), wstr.end());

To see a detailed explanation, please refer to the following post: std::wstring VS std::string

Community
  • 1
  • 1
gongzhitaao
  • 6,566
  • 3
  • 36
  • 44
  • 3
    It might work for ASCII text, but this is very locale-dependant and won't work for some character encodings. – user657267 Apr 02 '13 at 02:25