判断目录是否存在

写日志时常用到log目录,可调用DIR *opendir(const char * pathname);函数判断指定目录是否存在,若不存在则使用int mkdir(const char * pathname, mode_t mode);创建该目录,还给出了目录操作的小程序示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

判断目录是否存在

       写日志时,经常可能需要用到log这样的目录,通过调用

    DIR *opendir(const char * p a t h n a m e) ;

       这个函数来判断指定目录是否存在,不存在则使用

    int mkdir(const char * p a t h n a m e, mode_t m o d e)

    来创建该目录。下面一个小程序便是对目录操作的一个例子:

 

#include <sys/types.h>

#include <sys/stat.h>

#include <dirent.h>

#include <stdio.h>

#include <stdlib.h>

 

int main(int argc, char *argv[])

{

    if(argc != 2)

    {

        fprintf(stderr, "usage: dir director/n");

        return -1;

    }

   

    DIR *dir = opendir(argv[1]);

    if(dir == NULL)

    {

        printf("%s is not director/n", argv[1]);

        //creae a director

        mkdir(argv[1], S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);

        printf("make director %s/n", argv[1]);

        return -1;

    }

   

    printf("%s is exist./n", argv[1]);

    closedir(dir);

    return 0;

}

 

 

 

 

### C++ 判断目录是否存在的方法 在 C++ 中,判断目录是否存在的实现通常依赖于标准库 `<filesystem>` 或者 POSIX 提供的相关 API。以下是两种常见的实现方式及其代码示例。 --- #### 1. 使用 `<filesystem>`(C++17 及以上) 自 C++17 引入 `<filesystem>` 标准库以来,判断目录是否存在变得非常简单直观。该方法跨平台支持良好,推荐优先使用。 ```cpp #include <iostream> #include <filesystem> bool is_directory_exists(const std::string& path) { return std::filesystem::exists(path) && std::filesystem::is_directory(path); } int main() { std::string dir_path = "/path/to/directory"; // 替换为目标路径 if (is_directory_exists(dir_path)) { std::cout << "Directory exists." << std::endl; } else { std::cout << "Directory does not exist." << std::endl; } return 0; } ``` 此代码中: - `std::filesystem::exists` 检查路径是否存在。 - `std::filesystem::is_directory` 进一步确认路径是否目录[^3]。 --- #### 2. 使用 POSIX 函数 (`stat`) 对于不支持 C++17 的编译器环境,或者需要更底层控制的情况下,可以采用 POSIX 提供的 `stat` 函数来检测目录存在状态。 ```cpp #include <iostream> #include <sys/stat.h> #include <unistd.h> bool is_directory_exists(const char* path) { struct stat info; if (stat(path, &info) != 0) { return false; // 路径不存在或无法访问 } return (info.st_mode & S_IFDIR) != 0; // 检查是否目录 } int main() { const char* dir_path = "/path/to/directory"; // 替换为目标路径 if (is_directory_exists(dir_path)) { std::cout << "Directory exists." << std::endl; } else { std::cout << "Directory does not exist." << std::endl; } return 0; } ``` 此代码中: - `stat` 函数获取路径的状态信息并存储到结构体 `struct stat` 中。 - `(info.st_mode & S_IFDIR)` 用于验证路径是否具有目录属性[^4]。 --- #### 3. Windows 平台下的实现 如果目标平台是 Windows,还可以使用 `_access_s` 函数配合标志位检查路径性质。 ```cpp #include <iostream> #include <io.h> // _access_s 定义于此头文件 bool is_directory_exists(const char* path) { DWORD attributes = GetFileAttributesA(path); if (attributes == INVALID_FILE_ATTRIBUTES) { return false; // 路径无效或不可访问 } return (attributes & FILE_ATTRIBUTE_DIRECTORY) != 0; // 检查是否目录 } int main() { const char* dir_path = "C:\\path\\to\\directory"; // 替换为目标路径 if (is_directory_exists(dir_path)) { std::cout << "Directory exists." << std::endl; } else { std::cout << "Directory does not exist." << std::endl; } return 0; } ``` 此代码中: - `GetFileAttributesA` 返回文件或目录的属性值。 - `FILE_ATTRIBUTE_DIRECTORY` 是一个特定标志,表示路径指向的是目录[^5]。 --- ### 总结 三种方法各有适用场景: - 如果项目能够使用 C++17 或更高版本的标准库,则建议首选 `<filesystem>` 方案,因其简洁易懂且具备良好的可移植性。 - 对于低级操作系统交互需求较高的场合,POSIX 的 `stat` 和 Windows 的 `GetFileAttributesA` 更加适合,但需要注意平台差异带来的额外复杂度。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值