使用filesystem :: path,如何以跨平台方式打开文件? [英] Using a filesystem::path, how do you open a file in a cross-platform way?

查看:167
本文介绍了使用filesystem :: path,如何以跨平台方式打开文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设您使用了新的 std :: filesystem (或 std :: experimental :: filesystem )代码来查找文件。您有一个 path 变量,其中包含此变量的完整路径名。



如何打开该文件? / p>

这听起来很愚蠢,但请考虑以下明显的答案:

  std :: filesystem :: path my_path = ...; 
std :: ifstream stream(my_path.c_str(),std :: ios :: binary);

无法保证正常工作。为什么?因为例如在Windows上, path :: string_type std :: wstring 。因此 path :: c_str 将返回 const wchar_t * 。并且 std :: ifstream 只能 采用 const char * 类型的路径。 / p>

现在事实证明,此代码实际上将在VS中起作用。为什么?因为Visual Studio具有库扩展名,该扩展名确实可以正常工作。但这是非标准行为,因此不是便携式。例如,我不知道Windows上的GCC是否提供相同的功能。



您可以尝试以下方法:

  std :: filesystem :: path my_path = ...; 
std :: ifstream stream(my_path.string()。c_str(),std :: ios :: binary);

只有Windows再次使我们困惑。因为如果 my_path 恰好包含Unicode字符,那么您现在就可以正确设置Windows ANSI区域设置了。如果路径碰巧具有来自多种语言的字符,而这些语言不能在同一ANSI语言环境中存在,那么即使这样也不一定能为您省钱。



Boost Filesystem实际上也有类似的问题。但是他们扩展了iostream的版本以直接支持 path s。



我在这里缺少什么吗?委员会是否添加了跨平台文件系统库而不向其中的 open 文件添加跨平台方式?

解决方案

Bo Persson指出,这是标准库缺陷报告。此缺陷已解决,C ++ 17将发布,要求 path :: value_type 不是 char 的实现除常规的 const char * 版本外,使其文件流类型还采用 const文件系统路径:: value_type *


Let's say you have used the new std::filesystem (or std::experimental::filesystem) code to hunt down a file. You have a path variable that contains the full pathname to this variable.

How do you open that file?

That may sound silly, but consider the obvious answer:

std::filesystem::path my_path = ...;
std::ifstream stream(my_path.c_str(), std::ios::binary);

This is not guaranteed to work. Why? Because on Windows for example, path::string_type is std::wstring. So path::c_str will return a const wchar_t*. And std::ifstream can only take paths with a const char* type.

Now it turns out that this code will actually function in VS. Why? Because Visual Studio has a library extension that does permit this to work. But that's non-standard behavior and therefore not portable. For example, I have no idea if GCC on Windows provides the same feature.

You could try this:

std::filesystem::path my_path = ...;
std::ifstream stream(my_path.string().c_str(), std::ios::binary);

Only Windows confounds us again. Because if my_path happened to contain Unicode characters, then now you're reliant on setting the Windows ANSI locale stuff correctly. And even that won't necessarily save you if the path happens to have characters from multiple languages that cannot exist in the same ANSI locale.

Boost Filesystem actually had a similar problem. But they extended their version of iostreams to support paths directly.

Am I missing something here? Did the committee add a cross-platform filesystem library without adding a cross-platform way to open files in it?

解决方案

Bo Persson pointed out that this is the subject of a standard library defect report. This defect has been resolved, and C++17 will ship, requiring implementations where path::value_type is not char to have their file stream types take const filesystem path::value_type*s in addition to the usual const char* versions.

这篇关于使用filesystem :: path,如何以跨平台方式打开文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆