如何测试提供的路径是否包含文件或只是一个目录



为什么boost::is_regular_file返回false?我正在wandbox.org 上运行以下代码

#include <boost/filesystem.hpp>
#include <iostream>
namespace filesys = boost::filesystem;
int main(){
filesys::path pathObj("used/for/test/DoesNotExist.json");
std::cout <<"is file "<< filesys::is_regular_file(pathObj) << std::endl;
return 0;
}

这是指定的行为。

is_regular_file(p)等效于s.type() == file_type::regular

注释
如果status(p)将抛出,则会额外指定抛出过载以抛出std::filesystem::filesystem_error

status(p)
如果p不存在,则返回file_status(file_type::not_found)

file_type::not_found == file_type::regular为false。

我想测试它是否是一个包含文件的路径。不管文件是否存在。Just Is–Hani Gotc34分钟前

除非存在文件,否则可以是其中之一。它甚至可以是fifo、设备节点或UNIX域套接字。这是因为名称与类型无关。

简单演示:Coliru直播

#include <boost/filesystem.hpp>
#include <iostream>
#include <iomanip>
namespace fs = boost::filesystem;
using boost::filesystem::path;
static inline std::ostream& operator<<(std::ostream& os, fs::file_type t) {
switch (t) {
case fs::file_type::block_file:     return os << "block_file";
case fs::file_type::character_file: return os << "character_file";
case fs::file_type::directory_file: return os << "directory_file";
case fs::file_type::fifo_file:      return os << "fifo_file";
case fs::file_type::file_not_found: return os << "file_not_found";
case fs::file_type::regular_file:   return os << "regular_file";
case fs::file_type::reparse_file:   return os << "reparse_file";
case fs::file_type::socket_file:    return os << "socket_file";
case fs::file_type::symlink_file:   return os << "symlink_file";
case fs::file_type::status_unknown: return os << "unknown/status error";
case fs::file_type::type_unknown:
default: return os << "unknown";
}
}
#define ACTION(statement)                                                      
statement;                                                                 
std::cout << " ----- " << std::setw(50) << #statement << " "               
<< std::boolalpha;                                               
std::cout << "type(): " << status(spec).type() << std::endl;               
// std::cout << "is_regular_file(): " << is_regular_file(spec) << std::endl;
int main(){
ACTION(path spec("used/for/test/DoesNotExist.json"));
ACTION(create_directories(spec.parent_path()));
ACTION(create_directory(spec));
ACTION(remove(spec));
ACTION(std::ofstream(spec.native()));
ACTION(remove(spec));
path temp = fs::unique_path("/tmp/stackoverflow-%%%%%.tmp");
std::cout << "(temp = " << temp << ")n";
ACTION(create_symlink(temp, spec));
ACTION(std::ofstream(temp.native()));
ACTION(remove(temp));
ACTION(remove(spec));
ACTION(create_symlink(fs::temp_directory_path(), spec));
ACTION(remove(spec));
}

打印,例如

-----       path spec("used/for/test/DoesNotExist.json") type(): file_not_found
-----             create_directories(spec.parent_path()) type(): file_not_found
-----                             create_directory(spec) type(): directory_file
-----                                       remove(spec) type(): file_not_found
-----                       std::ofstream(spec.native()) type(): regular_file
-----                                       remove(spec) type(): file_not_found
(temp = "/tmp/stackoverflow-e4bcc.tmp")
-----                         create_symlink(temp, spec) type(): file_not_found
-----                       std::ofstream(temp.native()) type(): regular_file
-----                                       remove(temp) type(): file_not_found
-----                                       remove(spec) type(): file_not_found
-----    create_symlink(fs::temp_directory_path(), spec) type(): directory_file
-----                                       remove(spec) type(): file_not_found

正如您所看到的,文件类型会根据情况而变化。

备选方案

也许您想响应目录文件名没有扩展名的常见命名约定:

在Coliru上直播

#include <boost/filesystem.hpp>
#include <iostream>
using boost::filesystem::path;
int main() {
for (path p : {
"used/for/test/DoesNotExist.json",
"used/for/test",
"used/for/test/",
})
std::cout << p << " has extension: " << std::boolalpha
<< p.has_extension() << " (" << p.extension() << ")n";
}

打印

"used/for/test/DoesNotExist.json" has extension: true (".json")
"used/for/test" has extension: false ("")
"used/for/test/" has extension: false ("")

相关内容

最新更新