我正在使用unicode支持来编译我的库,现在遇到的问题是我需要打开一个ASCII数据文件,但是文件名当然是用widechars。那我该怎么做呢?

当我使用此代码时:

std::wstring s = L"path";
std::ifstream fl;
fl.open(s.c_str(), std::fstream::in);


然后我得到这个错误:

error: no matching function for call to 'std::basic_ifstream<char>::open(const wchar_t*, const openmode&)'


当我使用

std::wifstream fl;
fl.open(s.c_str(), std::fstream::in);


我犯了同样的错误 (???)

error: no matching function for call to 'std::basic_ifstream<wchar_t>::open(const wchar_t*, const openmode&)'


即使这样,当我使用std::wifstream时,流是否也希望输入数据也采用unicode?如果是这样,那么我将不得不使用std::ifstream,但是我仍然需要将宽字符串传递给它,或者以某种方式将其转换为ASCII。正确的方法是什么?

我正在使用MingW gcc 4.8.3。

mingw32-g++.exe -pedantic -std=c++11 -Wextra -Wall -g -D_UNICODE -D_WIN32 -Iinclude -c propertyfile.cpp -o propertyfile.o

最佳答案

像这样的东西:

std::wstring s = L"path";
std::string ansi( begin(s), end(s) );
std::ifstream fl ( ansi );


如果文件是ASCII,则不能使用wifstream。

请注意,仅当路径为纯ASCII时它才有效。

关于c++ - 打开具有Unicode名称但ASCII数据的输入文件流,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21456443/

10-13 08:57