简体   繁体   English

为 char16_t 寻找 _wfindfirst 的 c++ 等效项

[英]Looking for c++ equivalent for _wfindfirst for char16_t

I have filenames using char16_t characters:我有使用char16_t字符的文件名:

char16_t Text[2560] = u"ThisIsTheFileName.txt";
char16_t const* Filename = Text;

How can I check if the file exists already?如何检查文件是否已经存在? I know that I can do so for wchar_t using _wfindfirst() .我知道我可以使用_wfindfirst()wchar_t这样做。 But I need char16_t here.但我在这里需要char16_t

Is there an equivalent function to _wfindfirst() for char16_t ? char16_t是否有与_wfindfirst()等效的功能?

Background for this is that I need to work with Unicode characters and want my code working on Linux (32-bit) as well as on other platforms (16-bit).这样做的背景是我需要使用 Unicode 字符并希望我的代码在 Linux(32 位)以及其他平台(16 位)上工作。

findfirst() is the counterpart to _wfindfirst() . findfirst()_wfindfirst()的对应物。

However, both findfirst() and _wfindfirst() are specific to Windows.但是, findfirst()_wfindfirst()都特定于 Windows。 findfirst() accepts ANSI (outdated legacy stuff). findfirst()接受 ANSI(过时的旧东西)。 _wfindfirst() accepts UTF-16 in the form of wchar_t (which is not exactly the same thing as char16_t ). _wfindfirst()wchar_t的形式接受 UTF-16(这与char16_t不完全相同)。

ANSI and UTF-16 are generally not used on Linux. Linux 上一般不使用 ANSI 和 UTF-16。 findfirst() / _wfindfirst() are not included in the gcc compiler. findfirst() / _wfindfirst()不包含在 gcc 编译器中。

Linux uses UTF-8 for its Unicode format. Linux 使用 UTF-8 作为其 Unicode 格式。 You can use access() to check for file permission, or use opendir() / readdir() / closedir() as the equivalent to findfirst() .您可以使用access()检查文件权限,或使用opendir() / readdir() / closedir()作为findfirst()的等效项。

If you have a UTF-16 filename from Windows, you can convert the name to UTF-8, and use the UTF-8 name in Linux.如果您有来自 Windows 的 UTF-16 文件名,您可以将名称转换为 UTF-8,并在 Linux 中使用 UTF-8 名称。 See How to convert UTF-8 std::string to UTF-16 std::wstring?请参阅如何将 UTF-8 std::string 转换为 UTF-16 std::wstring?

Consider using std::filesystem in C++17 or higher.考虑在 C++17 或更高版本中使用std::filesystem

Note that a Windows or Linux executable is 32-bit or 64-bit, that doesn't have anything to do with the character set.请注意,Windows 或 Linux 可执行文件是 32 位或 64 位,与字符集没有任何关系。 Some very old systems are 16-bit, you probably don't come across them.一些非常古老的系统是 16 位的,您可能不会遇到它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM