在 Ubuntu 中的 C++ 中检查并创建目录
来源:http://www.tudoupe.com时间:2021-11-06
C++ 在 Ubuntu 下, 检查文件夹是否存在并创建一个新文件夹 。
查看是否存在
对于信头,请使用 unistd.Access 函数。
#include<unistd.h>
根据 h 函数:
/* Values for the second argument to access. These may be OR'd together. */ #define R_OK 4 /* Test for read permission. */ #define W_OK 2 /* Test for write permission. */ #define X_OK 1 /* Test for execute permission. */ #define F_OK 0 /* Test for existence. */ /* Test for access to NAME using the real UID and real GID. */ extern int access (const char *__name, int __type) __THROW __nonnull ((1));
所输入的两个变量是文件夹的位置和要搜索的权限,它们正在读写中。
std::string folderpath = "/<the_folder_path>";
if(access(folderpath.c_str(), 6)){
std::cout<<"folder does not exist! Will create a new one!" <<std::endl;
}
创建文件夹
使用stat.h中的mkdir函数
#include<sys/stat.h> // 注意:这个头文件在sys下面
截取stat.h函数声明如下:
/* Create a new directory named PATH, with permission bits MODE. */
extern int mkdir (const char *__path, __mode_t __mode)
__THROW __nonnull ((1));
例如,使用比特作为计量单位的授权产生的路径名称和值是输入的两个参数。
mkdir(folderpath.c_str(), 0777);
注意
由于两种方法都指定了 char* 中指定的路径名称类型, std : 必须使用字符串类型文件名 。 c_str ()
相关新闻
- 2023-05-06 微pe怎么初始化U盘(微pe怎么恢复初
- 2023-05-06 Xp系统boot 进入pe(boot manager 怎么进入
- 2023-05-06 win pe修复bcdboot(pe修复系统)
- 2023-05-06 win7更新失败 pe(win7更新失败还原更
- 2023-05-06 u盘装了pe读取不了(u盘能进pe读取不
- 2023-05-06 u盘pe 发热(u盘发热烫手)
- 2023-05-06 u盘pe下看不到硬盘(u盘启动pe看不到
- 2023-05-06 pe盘 ntfs(u盘ntfs格式)
- 2023-05-06 sony笔记本进入pe模式(联想笔记本怎
- 2023-05-06 pe启动盘进不去(pe启动盘进不去系统
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
