- 一、path类的操作
一、path类的操作
#include#include #include int main() { namespace fs = std::filesystem; //定义路径,使用生字符串、转移字符串、正斜杠字符串 fs::path p1{ "E:\C_pp\hello.txt" }; fs::path p2{ R"(E:C_pp)" }; fs::path p3{ "E:C_pp/hello.txt" }; //输出默认文件分隔符 std::cout << "file separator is: " << fs::path::preferred_separator << std::endl; //判断是否是常规文件,如果是,输出文件大小 if (fs::is_regular_file(p2)) { std::cout << p2 << "s size is : " << fs::file_size(p2) << std::endl; } //判断是否是目录,如果是目录,列出其子目录 else if (fs::is_directory(p2)) { std::cout << p2 << " is a directory,includes: " << std::endl; for (auto& e : fs::directory_iterator(p2)) { std::cout << " " << e.path(); } } //判断路径是否存在 else if (fs::exists(p2)) { std::cout << p2 << " is a special filen "; } else { std::cout << p2 << " does not exist " << std::endl; } std::cin.get(); }
结果如上图所示。



