Search code examples
unicodewchar-tequivalentfindfirstchar16-t

Looking for c++ equivalent for _wfindfirst for char16_t


I have filenames using char16_t characters:

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(). But I need char16_t here.

Is there an equivalent function to _wfindfirst() for char16_t?

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).


Solution

  • findfirst() is the counterpart to _wfindfirst().

    However, both findfirst() and _wfindfirst() are specific to Windows. findfirst() accepts ANSI (outdated legacy stuff). _wfindfirst() accepts UTF-16 in the form of wchar_t (which is not exactly the same thing as char16_t).

    ANSI and UTF-16 are generally not used on Linux. findfirst()/_wfindfirst() are not included in the gcc compiler.

    Linux uses UTF-8 for its Unicode format. You can use access() to check for file permission, or use opendir()/readdir()/closedir() as the equivalent to 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. See How to convert UTF-8 std::string to UTF-16 std::wstring?

    Consider using std::filesystem in C++17 or higher.

    Note that a Windows or Linux executable is 32-bit or 64-bit, that doesn't have anything to do with the character set. Some very old systems are 16-bit, you probably don't come across them.