31356

SHGetFolderPathW not working with japanese username

Question:

Im trying to create one file in %APPDATA% using SHGetFolderPathW function. This function I guess is getting chars in unicode. I'm working with Visual Studio 2010 little project. The following code work in english version of win 8 but not in japanese version (username is japanese):

#include "stdafx.h" #include <iostream> #include <fstream> #include <windows.h> #include <Shlobj.h> #include <tchar.h> #include <string> int _tmain(int argc, _TCHAR* argv[]) { std::wstring output = L""; WCHAR* folder = new WCHAR[2048]; SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, folder ); std::wstring str1 = folder; str1 += L"\\hola.txt"; std::wcout << str1 << std::endl; std::string str(str1.begin(), str1.end()); std::cout << str << std::endl; // Create file in folder FILE * file; char *path = new char[str.length()+1]; strcpy(path, str.c_str()); file = fopen (path, "w"); fputs ("Hello World",file); fclose (file); system("PAUSE"); return 0; }

The code is showing good path in English version, but in japanese, this path isn't right. I wonder if I have any way to use SHGetFolderPath in all languages. I'm googling two days but not solution found.

Answer1:

If you have a wide string file path, use wide string version of fopen. This should work:

#include <string> #include <stdio.h> #include <Shlobj.h> #include <tchar.h> int _tmain(int argc, _TCHAR* argv[]) { WCHAR folder[MAX_PATH]; SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, folder ); std::wstring str1 = folder; str1 += L"\\hola.txt"; // Create file in folder FILE * file; file = _wfopen (str1.c_str(), L"w"); fputs ("Hello World",file); fclose (file); return 0; }

Recommend