定义一个类CheckPath,它由两个public方法组成:
1) checkPath:检查传入的字符串指定的路径是否存在,存在返回true,否则返回false。
2) createFilePath:根据传入的字符串指定的路径,创建由 pathDepth指定的路径,例如:如果传入字符串为/home/user/aa/bb,如果指定的pathDepath为0,则不创建这个路径,如果pathDepath为1,则创建从/开始创建所有路径,如果pathDepath为2,则创建从/home开始创建所有路径。
- class CheckPath{
- public:
- bool checkPath(std::string &filePath);
- bool createFilePath(const char * path, int pathDepth);
- };
checkpath.cpp源文件:
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include
-
- #define MAX_FILENAME_LEN 256
-
- class CheckPath{
- public:
- bool checkPath(std::string &filePath);
- bool createFilePath(const char * path, int pathDepth);
- };
-
- /* 测试指定目录是否存在 */
- bool CheckPath::checkPath(std::string &filePath)
- {
- char lastChar;
- struct stat buff;
- int istat;
- int isDir = 0;
- bool pathExists = false;
-
- int len = filePath.size();
-
- if (len == 0) return false;
-
- lastChar = filePath[len - 1];
-
- if (lastChar == '/'){
- filePath.resize(len - 1);
- }
-
- istat = stat(filePath.c_str(), &buff);
-
- if (!istat) isDir = (S_IFDIR & buff.st_mode);
-
- if (!istat && isDir){
- pathExists = true;
- }
-
- filePath.append("/");
-
- return pathExists;
- }
-
- /* 安pathDepath指定深度开始创建指定的目录 */
- bool CheckPath::createFilePath(const char * path, int pathDepth)
- {
- bool result = true;
- char * parts[MAX_FILENAME_LEN];
- int num_parts;
- char directory[MAX_FILENAME_LEN];
- char nextDir[MAX_FILENAME_LEN];
- char * saveptr;
- int i = 0;
-
- /* donot create path */
- if (pathDepth == 0) return result;
-
- while ((path[i] == '/') && i < MAX_FILENAME_LEN){
- nextDir[i] = path[i];
- i++;
- }
-
- nextDir[i] = 0;
- printf("nextDir: %s\n", nextDir);
-
- strcpy(directory, &path[i]);
- printf("Directory: %s\n", directory);
-
- num_parts = 0;
- parts[num_parts] = strtok_r(directory, "\\/", &saveptr);
-
- while (parts[num_parts] != NULL){
- parts[++num_parts] = strtok_r(NULL, "\\/", &saveptr);
- }
-
- // for (i = 0; i < num_parts; i++){
- // printf("%d: %s\n", i+1, parts[i]);
- //}
-
- if (pathDepth < 0){
- pathDepth = num_parts + pathDepth;
- if (pathDepth < 1) pathDepth = 1;
- }
-
- for (i = 0; i < num_parts && result != false; i++){
- strcat(nextDir, parts[i]);
-
- if (i >= pathDepth)
- {
- // printf("depath = %d,create path %s\n",i, nextDir);
- if (mkdir(nextDir, 0777) != 0 && errno != EEXIST){
- result = false;
- }
- }
- strcat(nextDir, "/");
- }
-
- return result;
- }
-
- /* 测试主程序 */
- int main(int argc, char ** argv)
- {
- if (argc != 2){
- fprintf(stderr, "Usage: %s pathname\n", argv[0]);
- return -1;
- }
-
- std::string filePath = argv[1];
- std::cout << "Check File Path : " << filePath << " exists?" << std::endl;;
- CheckPath * pcp = new CheckPath();
-
- /* Test file path exists or not */
- std::cout << "*****************************************"<
- std::cout << "Test Function 1 checkPath:" << std::endl;
- if (pcp->checkPath(filePath))
- {
- std::cout << "FilePath:" << filePath <<" does exist" << std::endl;
- }
- else{
- std::cout << "FilePath:" << filePath << " does not exist" << std::endl;
- }
- std::cout << "*****************************************"<
-
- std::cout << "*****************************************"<
- std::cout << "Test Function 2 createFilePath:" << std::endl;
- std::cout << "Please enter a path to create:";
- std::cin >> filePath;
- std::cout << "The path to create :" << filePath << std::endl;
- int depth;
- std::cout << "Please enter the create depth:";
- std::cin >> depth;
- printf("Depth: %d\n", depth);
-
-
- if (pcp->createFilePath(filePath.c_str(), depth)){
- std::cout << "Create the " << depth << " depth of file path " << filePath << std::endl;
- }
- else{
- std::cout << "can not create file path " << filePath << std::endl;
- }
- std::cout << "*****************************************"<
-
- delete pcp;
-
- return 0;
- }
编译以上代码,观察结果:
- orangepi@orangepi5:~/C_program$ ./checkpath aa
- Check File Path : aa exists?
- *****************************************
- Test Function 1 checkPath:
- FilePath:aa/ does exist
- *****************************************
-
- *****************************************
- Test Function 2 createFilePath:
- Please enter a path to create:/home/orangepi/C_program/aa/bb
- The path to create :/home/orangepi/C_program/aa/bb
- Please enter the create depth:3
- Depth: 3
- nextDir: /
- Directory: home/orangepi/C_program/aa/bb
- Create the 3 depth of file path /home/orangepi/C_program/aa/bb
- *****************************************
- orangepi@orangepi5:~/C_program$ ls aa/
- bb
检查当前目录下aa目录时存在的,并且在aa目录下创建了一个bb目录。
-
相关阅读:
GB/T28181-2016 SDP定义和音视频传输模式解读
PAT:1020 Tree Traversals
思科设备端口聚合配置命令
【数据结构】归并排序
性能优化:JIT即时编译与AOT提前编译
Gitlab+P3C-PMD(阿里云插件)标准化你团队的代码和提交信息
剑指offer43-56树
13年老鸟整理,性能测试技术知识体系总结,从零开始打通...
客户端远程连接mysql服务问题记录
迭代+递归解决反转单链表问题
-
原文地址:https://blog.csdn.net/yuyuyuliang00/article/details/134069352