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

How to test if a provided path contains a file or it's just a directory

为什么 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) is equivalent to s.type() == file_type::regular

Notes
The throwing overload is additionally specified to throw std::filesystem::filesystem_error if status(p) would throw

status(p)
If p does not exist, returns file_status(file_type::not_found)

file_type::not_found == file_type::regular 为假。

I want to test if it's a path containing a file or not. Regardless if the file exists or not. Just Is – Hani Gotc 34 mins ago

除非文件存在,否则它可以是任何一个。它甚至可以是 fifo、设备节点或 UNIX 域套接字。那是因为名称对类型没有任何意义。

简单演示:Live On 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/Whosebug-%%%%%.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/Whosebug-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

如您所见,文件类型会根据情况发生变化。

备选

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

Live On 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 ("")