使用非 MSVC 编译器在 Windows 下打开带有 Unicode 文件名的文件的 fstream
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2316672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Opening fstream with file with Unicode file name under Windows using non-MSVC compiler
提问by Artyom
I need to open a file as std::fstream (or actually any other std::ostream) when file name is "Unicode" file name.
当文件名是“Unicode”文件名时,我需要将文件作为 std::fstream(或实际上任何其他 std::ostream)打开。
Under MSVC I have non-standardextension std::fstream::open(wchar_t const *,...)
? What can I do with other compilers like GCC (most important) and probably Borland compiler.
在 MSVC 下,我有非标准扩展名std::fstream::open(wchar_t const *,...)
?我可以用其他编译器做什么,比如 GCC(最重要的)和 Borland 编译器。
I know that CRTL provides _wfopen
but it gives C FILE *
interface instead of io-streams, maybe there is a non-standard way to create io-stream from FILE *
? Is there any boost::ifstream
with MSVC like extension for Windows?
我知道 CRTL 提供_wfopen
但它提供 CFILE *
接口而不是 io-streams,也许有一种非标准的方式来创建 io-stream FILE *
?有没有boost::ifstream
类似 MSVC 的 Windows 扩展?
采纳答案by Artyom
Currently there is no easy solution.
目前没有简单的解决方案。
You need to create your own stream buffer that uses _wfopen
under the hood. You can use for this for example boost::iostream
您需要创建自己的_wfopen
在后台使用的流缓冲区。例如,您可以为此使用boost::iostream
回答by Kornel Kisielewicz
Unfortunately, there's no standard way to do that, although C++0x (1x?) promises to do that. Until then, you properly assumed that a solution can be found in Boost, however, the library you're searching for is Boost.Filesystem.
不幸的是,没有标准的方法可以做到这一点,尽管 C++0x(1x?)承诺这样做。在那之前,您正确地假设可以在 Boost 中找到解决方案,但是,您要搜索的库是Boost.Filesystem。
Boost.Filesystem internally uses wide strings by default for its universal path system, so there are no unicode problems in this regard.
Boost.Filesystem 内部默认使用宽字符串作为其通用路径系统,因此在这方面不存在 unicode 问题。
回答by Michael Burr
Convert the Unicode filename to a char*
string using something like wcstombs()
or WideCharToMultiByte()
(which gives you far more control over the codepages involved).
char*
使用类似wcstombs()
或的东西将 Unicode 文件名转换为字符串WideCharToMultiByte()
(这使您可以更好地控制所涉及的代码页)。
Then use the converted filename to open the file.
然后使用转换后的文件名打开文件。