Special Characters C++

Hi all,

I have a problem with some special characters. When I go across the lines with the debug tool, I see the variable caminho (a std::string) with the expected values: "maçã", but the variables "stemp" and "aux" have squares instead of "ç" and "ã".

After the line 4 the program does´t break, but it stops working like expected.

1
2
3
4
wstring stemp = wstring(caminho.begin(), caminho.end());
LPCWSTR a = stemp.c_str();
hFind = FindFirstFile(a, &fd);
FindNextFile(hFind,&fd);  


I am using Visula Studio 2010. And the "Character Set" option is set to "Use Unicode Character Set".
Last edited on
1. Always check that your calls succeed.
If [FindFirstFile()] fails or fails to locate files from the search string in the lpFileName parameter, the return value is INVALID_HANDLE_VALUE and the contents of lpFindFileData are indeterminate.

2. What's happening is that Visual Studio is properly decoding the contents of caminho based on your system's locale with the correct code page, using MultiByteToWideChar(). If you want to use the Unicode functions you need to convert your narrow strings using that function.
3. However, it'd be much easier to simply call the ANSI functions instead:
1
2
3
hFind = FindFirstFileA(caminho.c_str(), &fd);
if (hFind == INVALID_HANDLE_VALUE)
    //Failed. Check GetLastError(). 

4. Do note that if you use the ANSI functions, your code will only work if the system is set to the correct locale. If someone in, say, Spain tries to run your program, it may not work.
Topic archived. No new replies allowed.