简体   繁体   中英

get case-correct file name on macOS

I am on macOS (where file names are case insensitive) and I need to find out the actual case-correct name of a file. For example I do not know whether my file has name A.txt or a.txt or A.TXT or a.TXT etc. I want to display the name correctly on the screen.

What is the fastest way in C/C++?

UPDATE: I tried <filesystem> and Qt framework too (my application is in Qt). But the only possible solution with these libraries seem to be to read full list all entries in the parent directory and then compare if any of these child items matches to the given name pattern. This is obviously not viable for performance reasons.

std::filesystem::canonical transforms the case of a file to the on disk path (at least on macOS and Visual Studio on Windows, I'm not sure this is a requirement of the standard):

With a file in the current directory called test.cpp the following program prints:

"/Users/user/test.cpp"
#include <filesystem>
#include <iostream>

int main()
{
  std::filesystem::path p("TEST.cpp");
  std::cout << std::filesystem::canonical(p) << "\n";
}

The same program works with absolute paths as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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