I have a multibyte Windows project where I try to access a file which can have name with any symbols modern Windows allows. But I fail miserably in case of file name which contains non ASCII characters (Japanese, Swedish, Russian, etc).
For example:
const char * filename_ = "C:\\testÖ.txt"
struct _finddata_t fd;
long fh = _findfirst(filename_, &fd);
At this point _findfirst()
fails.
What would be best solution here to support all possible file names? I read that _findfirst()
depends on system locale that was set when program was started. Well, I can change that for a certain one but how can I determine the needed locale for a filename in this case?
The project has to remain multibyte.
Did anyone solve such problem before?
Also I tried to use wide char conversion but no luck as well. Code example below:
debug_prnt("DEBUG: Checking existance of a file: %s\n", filename_);
struct _wfinddata_t ff;
size_t requiredSize = mbstowcs(NULL, filename_, 0);
wchar_t * filename = (wchar_t *)malloc((requiredSize + 1) * sizeof(wchar_t));
if (!filename)
{
debug_prnt("ERROR: Memory allocation failed\n");
return FALSE;
}
size_t size = mbstowcs(filename, filename_, requiredSize + 1);
if (size == (size_t)(-1))
{
debug_prnt("ERROR: Couldn't convert string--invalid multibyte character.\n");
return FALSE;
}
long fh = _wfindfirst(filename, &ff);
if (fh > 0)
debug_prnt("DEBUG: File exists\n");
else
debug_prnt("DEBUG: File does not exist %ls\n", filename);
free(filename);