如何在不使用任何特殊 API 的情况下打开路径或文件名包含 unicode 字符的文件并读取或写入其内容?如果可能的话,如何仅使用 std 库或仅使用 Windows API 来做到这一点?我确实尝试使用 std::wifstream 打开文件,如下面的代码示例所示,但它无法编译。看起来它不需要'const wchar_t *'参数而是'const char *'。我正在使用 Dev-C++ IDE 中包含的 TDM-GCC 4.7.1 编译器。
#ifndef UNICODE
#define UNICODE
#endif
...
#include <clocale>
#include <windows.h>
#include <fstream>
...
int main(int argc, char **argv)
{
setlocale(LC_ALL, "Polish_Poland.852") ;
...
fileCompare(first, second) ;
...
}
...
bool fileCompare(wstring first, wstring second) // This function doesn't compile !
{
using namespace std ;
wifstream fin0(first.c_str(), ios::binary) ;
wifstream fin1(second.c_str(), ios::binary) ;
...
}
一些完整的例子:
#ifndef UNICODE
#define UNICODE
#endif
#include <clocale>
#include <conio.h>
#include <windows.h>
#include <fstream>
#include <string>
#include <iostream>
using namespace std ;
bool fileCompare(wstring first, wstring second) ;
int main(int argc, char **argv)
{
setlocale(LC_ALL, "Polish_Poland.852") ;
wstring first, second ;
first = L"C:\\A.dat" ;
second = L"C:\\E.dat" ;
fileCompare(first, second) ;
getch() ;
return 0 ;
}
bool fileCompare(wstring first, wstring second) // This function doesn't compile !
{
wifstream fin0(first.c_str(), ios::binary) ;
wifstream fin1(second.c_str(), ios::binary) ;
}
此外,当我将 L"C:\A.dat" 和 L"C:\E.dat" 替换为包含波兰字符的字符串时,它会输出有关非法字节序列的错误。