在 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 ()
相关新闻
- 2022-01-28 S32DS——PE调试器使用
- 2022-01-28 华硕M2N-MX SE主板如何进入bios设置
- 2022-01-28 华硕B85-A主板如何是通过bios设置u盘
- 2022-01-27 ltraISO 生成u盘启动盘后处理事项
- 2022-01-27 华硕P5QL-CM主板如何通过bios设置u盘
- 2022-01-27 微pe怎么安装原版win11 微pe安装原版
- 2022-01-26 win10 esd系统怎么用pe安装
- 2022-01-26 电脑系统装机教程图解(电脑装机
- 2022-01-25 非mbr怎么激活
- 2022-01-25 eprime安装教程
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
