简体   繁体   中英

Conversion from 'wchar_t' to 'const _Elem', possible loss of data

TCHAR buf[MAX_PATH];
int aFolder = CSIDL_APPDATA;
if (SHGetFolderPath(NULL, aFolder, NULL, SHGFP_TYPE_CURRENT, buf) != S_OK)
    *buf = '\0';

// buf     wchar_t
std::wstring str(&buf[0]); //convert to wstring
std::string str2(str.begin(), str.end()); //and convert to string.

std::string str3 = "\\settings.ini";
auto dir = str2 + str3;

warning:

conversion from 'wchar_t' to 'const _Elem', possible loss of data
1>          with
1>          [
1>              _Elem=char
1>          ]

What other way can I do this conversion without "possible loss of data" ? I have also tried to use SHGetFolderPathW() but it returns the same warning.

std::wstring is usually either UTF-16 or UTF-32. std::string is usually UTF-8. What you are doing is not a proper way of converting between those encodings. See UTF8 to/from wide char conversion in STL

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM