stdfilesystemcanonical
std::filesystem::canonical is a function provided by the C++ Standard Library's <filesystem> header. It returns the absolute path of a given file or directory, resolving any symbolic links, relative paths, or "." and ".." references.
Here's an example usage:
#include <iostream>
#include <filesystem>
int main() {
std::filesystem::path path = "my_directory/../my_file.txt";
std::filesystem::path canonicalPath = std::filesystem::canonical(path);
std::cout << "Original path: " << path << std::endl;
std::cout << "Canonical path: " << canonicalPath << std::endl;
return 0;
}
Output:
Original path: my_directory/../my_file.txt
Canonical path: /path/to/my_file.txt
In this example, the original path contains a ".." reference to the parent directory, but std::filesystem::canonical resolves it to the absolute path of the file
原文地址: https://www.cveoy.top/t/topic/iasD 著作权归作者所有。请勿转载和采集!