我正在尝试在 C 中打开文件,但是当文件在 Windows 中包含拉丁字符时,我遇到了问题。
这段代码
hFile = CreateFileW(ws, // file to be opened
GENERIC_READ, // open for reading
FILE_SHARE_READ, // share for reading
NULL, // default security
OPEN_EXISTING, // open existing file only
FILE_ATTRIBUTE_NORMAL |FILE_ATTRIBUTE_ARCHIVE | SECURITY_IMPERSONATION,
// normal file archive and impersonate client
NULL); // no attr. template
if(hFile == INVALID_HANDLE_VALUE)
printf("Could not open %ls file, error %d\n", ws, GetLastError());
else
printf("File's HANDLE is OK!\n");
// when finished, close the file handle
CloseHandle(hFile);
当文件没有任何奇怪的字符但失败并出现错误 2 (ERROR_FILE_NOT_FOUND) 时,它可以完美运行。
--
例如,使用此文件:
C:\Documents and Settings\Administrador\Escritorio\hola.mp3
输出是
File's HANDLE is OK!
但是有了这个文件:
C:\Documents and Settings\Administrador\Escritorio\holá.mp3
输出是
Could not open C:\Documents and Settings\Administrador\Escritorio\holá.mp3 file, error 2
这两个文件都存在于该位置。
--
这是ws的初始化:
char* filename;
wchar_t ws[256];
// I get the filename from the SDK I am using (Cycling'74 Max SDK)
filename = (atom_getsym(argv))->s_name;
// and convert it to UTF16
AnsiToUnicode16(filename, ws, 256);
AnsiToUnicode16
用于MultiByteToWideChar
进行转换。
--
当我使用 FindFirstFile() 遍历文件夹的文件时,我得到以下结果:
- 下一个文件名为 hola.mp3。
- 下一个文件名为 hol□.mp3。
我不知道如何让它知道hol□.mp3
应该是holá.mp3
。
顺便说一句,如果文件夹是带有重音符号的文件夹,则 FindFirstFile() 失败。