So, I am trying to write a program that will find files in a directory and traverse through them in random order. I'm pretty sure my code for finding the file paths is valid. I am storing these file paths in wchar_t arrays of size MAX_PATH.
When the file paths I am using contain all ASCII characters, everything goes smoothly. However, when encountering a file path with a Unicode character in it, the program blows up. Here is my process, really.
1. Find the file; with each new file found, write its full path to a .txt file.
2. Create a vector with a size equal to the number of files found and shuffle it.
3. Go to each line in my .txt file in an order determined by the shuffling of the vector.
This is the code I am using to try to write to the file:
m_tempFileListOut is a wofstream and fullPathBuffer is a wchar_t array containing the file path I want to write to the file. However, when I run this code, it will write file paths just fine up until it encounters a Unicode character and then stops outputting altogether.
Is there something I am doing wrong? Thanks in advance for any insight.
Is m_tempFileListOut an std::fstream? I'm surprised that compiled at all.
You need to somehow encode the string into a coherent representation. Just calling file.write((char *)path,length*sizeof(wchar_t)); will work for most cases, but it will not be portable, since there's no telling what representation the system uses (UCS-2? UTF-16? What endianness?).
UTF-8 is a portable alternative. I posted a conversion routine some time ago (http://www.cplusplus.com/forum/general/12288/ ). You can use it if you want.