是否有标准方法使用 Unicode 字符串文件路径执行 fopen?

发布于 2024-07-11 02:01:06 字数 55 浏览 5 评论 0原文

是否有标准方法使用 Unicode 字符串文件路径执行 fopen

Is there a standard way to do an fopen with a Unicode string file path?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

下壹個目標 2024-07-18 02:01:06

不,没有标准方法。 操作系统之间存在一些差异。 以下是不同操作系统处理非 ASCII 文件名的方式。

Linux

在Linux 下,文件名只是一个二进制字符串。 大多数现代发行版的约定是对非 ASCII 文件名使用 UTF-8。 但一开始,将文件名编码为 ISO-8859-1 是很常见的。 基本上由每个应用程序选择编码,因此您甚至可以在同一文件系统上使用不同的编码。 LANG 环境变量可以提示您首选编码是什么。 但如今,您可能可以假设到处都是 UTF-8。

但这并非没有问题,因为包含无效 UTF-8 序列的文件名在大多数 Linux 文件系统上完全有效。 如果您只支持 UTF-8,您将如何指定这样的文件名? 理想情况下,您应该同时支持 UTF-8 和二进制文件名。

OS X

OS X 上的 HFS 文件系统在内部使用 Unicode (UTF-16) 文件名。 大多数 C(和 POSIX)库函数(例如 fopen)接受 UTF-8 字符串(因为它们是 8 位兼容)并在内部进行转换。

Windows

Windows API 使用 UTF-16 作为文件名,但 fopen 使用当前代码页,无论它是什么(UTF-8 刚刚成为一个选项)。 许多 C 库函数都有一个接受 UTF-16 的非标准等效函数(Windows 上的 wchar_t)。 例如,_wfopen 而不是 fopen

No, there's no standard way. There are some differences between operating systems. Here's how different OSs handle non-ASCII filenames.

Linux

Under Linux, a filename is simply a binary string. The convention on most modern distributions is to use UTF-8 for non-ASCII filenames. But in the beginning, it was common to encode filenames as ISO-8859-1. It's basically up to each application to choose an encoding, so you can even have different encodings used on the same filesystem. The LANG environment variable can give you a hint what the preferred encoding is. But these days, you can probably assume UTF-8 everywhere.

This is not without problems, though, because a filename containing an invalid UTF-8 sequence is perfectly valid on most Linux filesystems. How would you specify such a filename if you only support UTF-8? Ideally, you should support both UTF-8 and binary filenames.

OS X

The HFS filesystem on OS X uses Unicode (UTF-16) filenames internally. Most C (and POSIX) library functions like fopen accept UTF-8 strings (since they're 8-bit compatible) and convert them internally.

Windows

The Windows API uses UTF-16 for filenames, but fopen uses the current codepage, whatever that is (UTF-8 just became an option). Many C library functions have a non-standard equivalent that accepts UTF-16 (wchar_t on Windows). For example, _wfopen instead of fopen.

梦里的微风 2024-07-18 02:01:06

在 *nix 中,您只需使用标准的 fopen (请参阅 TokeMacGuy 的回复或在此 论坛)
在 Windows 中,您可以使用 _wfopen,然后传递 Unicode 字符串(有关详细信息,请参阅 MSDN)。

由于没有真正通用的方法,我会将此调用与所有其他依赖于系统的函数一起包装在宏中。

In *nix, you simply use the standard fopen (see more information in reply from TokeMacGuy, or in this forum)
In Windows, you can use _wfopen, and then pass a Unicode string (for more information, see MSDN).

As there is no real common way, I would wrap this call in a macro, together with all other system-dependent functions.

断舍离 2024-07-18 02:01:06

这是您当前区域设置的问题。 在我的支持 Unicode 的系统上,文件路径将采用 Unicode。 我可以通过 locale 命令检测到这一点:

$ locale
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"

文件路径的编码通常在系统范围内设置,因此如果您的文件路径不在系统的区域设置中,您将需要转换它,也许可以通过 < href="http://www.gnu.org/software/libiconv/" rel="nofollow noreferrer">iconv 库。

This is a matter of your current locale. On my system, which is Unicode-enabled, file paths will be in Unicode. I'm able to detect this by means of the locale command:

$ locale
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"

The encoding of file paths is normally set system wide, so if your file path is not in the system's locale, you will need to convert it, perhaps by means of the iconv library.

晌融 2024-07-18 02:01:06

现在几乎所有 POSIX 平台都使用 UTF-8。 现代 Windows 还支持 UTF-8 作为区域设置,您可以在任何地方使用 UTF-8 并打开任何文件,而无需使用Windows 上的宽字符串。 fopen 只是可移植地工作

setlocale(LC_ALL, "en_us.utf8"); // need some setup before calling this
fopen(R"(C:\filê\wíth\Ünicode\name.txt)", "w+");

从 Windows 10 内部版本 17134(2018 年 4 月更新)开始,通用 C 运行时支持使用 UTF-8 代码页。 这意味着传递给 C 运行时函数的 char 字符串将需要 UTF-8 编码的字符串。 要启用 UTF-8 模式,请在使用 setlocale 时使用 ".UTF8" 作为代码页。 例如,setlocale(LC_ALL, ".UTF8") 将使用当前默认的 Windows ANSI 代码页 (ACP) 作为区域设置,并使用 UTF-8 作为代码页。

...

要在 Windows 10 之前的操作系统(例如 Windows 7)上使用此功能,您必须使用应用程序本地部署或使用 Windows SDK 版本 17134 或更高版本进行静态链接。 对于 17134 之前的 Windows 10 操作系统,仅支持静态链接。

UTF-8 支持

Almost all POSIX platforms use UTF-8 nowadays. And modern Windows also support UTF-8 as the locale, you can just use UTF-8 everywhere and open any files without using wide strings on Windows. fopen just works portably

setlocale(LC_ALL, "en_us.utf8"); // need some setup before calling this
fopen(R"(C:\filê\wíth\Ünicode\name.txt)", "w+");

Starting in Windows 10 build 17134 (April 2018 Update), the Universal C Runtime supports using a UTF-8 code page. This means that char strings passed to C runtime functions will expect strings in the UTF-8 encoding. To enable UTF-8 mode, use ".UTF8" as the code page when using setlocale. For example, setlocale(LC_ALL, ".UTF8") will use the current default Windows ANSI code page (ACP) for the locale and UTF-8 for the code page.

...

To use this feature on an OS prior to Windows 10, such as Windows 7, you must use app-local deployment or link statically using version 17134 of the Windows SDK or later. For Windows 10 operating systems prior to 17134, only static linking is supported.

UTF-8 Support

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文