I'm having trouble opening a file that has Unicode characters in its name. I created a file on my desktop with just a couple lines of text.
c:\users\james\desktop\你好世界.txt
EDIT: I'm using CLion. CLion is passing parameters in unicode.
When I put that string into the Windows run dialog, it finds the file and opens it.
Something interesting, though, is that I get double L'\\'
L'\\'
in the folder name from my call to CommandLineToArgvW:
L"c:\\\\users\\\\james\\\\desktop\\\\你好世界.txt"
So I wrote a small routine to copy the filename to another wchar_t *
and strip the slashes. Still doesn't work.
errno == 2
and f == NULL
.
size_t filename_max_len = wcslen(filename);
//strip double slashes
wchar_t proper_filename[MAX_PATH + 1];
wchar_t previous = L'\0';
size_t proper_filename_location = 0;
for(int x = 0; x < filename_max_len; ++x)
{
if(previous == L'\\' && filename[x] == L'\\')
continue;
previous = filename[x];
proper_filename[proper_filename_location++] = filename[x];
}
proper_filename[proper_filename_location] = L'\0';
//Read in binary mode to prevent the C system from screwing with line endings
FILE *f = _wfopen(proper_filename, L"rb");
int le = errno;
if (f == NULL)
{
perror(strerror(le));
if(le == ERROR_FILE_NOT_FOUND)
{
return DUST_ERR_FILE_NOT_FOUND;
}
else {
return DUST_ERR_COULD_NOT_OPEN_FILE;
}
}