<分区>
Possible Duplicate:
How to open an std::fstream (ofstream or ifstream) with a unicode filename ?
我想使用 C++ fstream 打开一个文本文件,但用于文件名的字符不属于 ASCII 字符集。 例如:
fstream fileHandle;
fileHandle.open("δ»Wüste.txt");
有没有办法可以打开具有此类名称的文件。
谢谢 维维克
标签 c++
<分区>
Possible Duplicate:
How to open an std::fstream (ofstream or ifstream) with a unicode filename ?
我想使用 C++ fstream 打开一个文本文件,但用于文件名的字符不属于 ASCII 字符集。 例如:
fstream fileHandle;
fileHandle.open("δ»Wüste.txt");
有没有办法可以打开具有此类名称的文件。
谢谢 维维克
最佳答案
来自问题How to open an std::fstream with a unicode filename @jalf 指出 C++ 标准库不支持 unicode,但是有一个 Windows 扩展可以接受 wchar_t 数组。
您将能够通过以 wchar_t 数组作为参数在 fstream 对象上创建或调用 open 来在 Windows 平台上打开文件。
fstream fileHandle(L"δ»Wüste.txt");
fileHandle.open(L"δ»Wüste.txt");
以上两个都将调用适当函数的 wchar_t* 版本,因为字符串上的 L 前缀表示它将被视为 unicode 字符串。
编辑:这是一个应该编译和运行的完整示例。我在我的电脑上创建了一个名为 δ»Wüste.txt
的文件内容This is a test.
然后我在同一目录中编译并运行了以下代码。
#include <fstream>
#include <iostream>
#include <string>
int main(int, char**)
{
std::fstream fileHandle(L"δ»Wüste.txt", std::ios::in|std::ios::out);
std::string text;
std::getline(fileHandle, text);
std::cout << text << std::endl;
system("pause");
return 0;
}
输出是:
This is a test.
Press any key to continue...
关于c++ - 使用 fstream 打开文本文件但文件名字符不在 ASCII 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5727791/