• C++程序练习


    定义一个类CheckPath,它由两个public方法组成:

    1) checkPath:检查传入的字符串指定的路径是否存在,存在返回true,否则返回false。

    2) createFilePath:根据传入的字符串指定的路径,创建由 pathDepth指定的路径,例如:如果传入字符串为/home/user/aa/bb,如果指定的pathDepath为0,则不创建这个路径,如果pathDepath为1,则创建从/开始创建所有路径,如果pathDepath为2,则创建从/home开始创建所有路径。

    1. class CheckPath{
    2. public:
    3.         bool checkPath(std::string &filePath);
    4.         bool createFilePath(const char * path, int pathDepth);
    5. };

    checkpath.cpp源文件: 

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #define MAX_FILENAME_LEN 256
    11. class CheckPath{
    12. public:
    13. bool checkPath(std::string &filePath);
    14. bool createFilePath(const char * path, int pathDepth);
    15. };
    16. /* 测试指定目录是否存在 */
    17. bool CheckPath::checkPath(std::string &filePath)
    18. {
    19. char lastChar;
    20. struct stat buff;
    21. int istat;
    22. int isDir = 0;
    23. bool pathExists = false;
    24. int len = filePath.size();
    25. if (len == 0) return false;
    26. lastChar = filePath[len - 1];
    27. if (lastChar == '/'){
    28. filePath.resize(len - 1);
    29. }
    30. istat = stat(filePath.c_str(), &buff);
    31. if (!istat) isDir = (S_IFDIR & buff.st_mode);
    32. if (!istat && isDir){
    33. pathExists = true;
    34. }
    35. filePath.append("/");
    36. return pathExists;
    37. }
    38. /* 安pathDepath指定深度开始创建指定的目录 */
    39. bool CheckPath::createFilePath(const char * path, int pathDepth)
    40. {
    41. bool result = true;
    42. char * parts[MAX_FILENAME_LEN];
    43. int num_parts;
    44. char directory[MAX_FILENAME_LEN];
    45. char nextDir[MAX_FILENAME_LEN];
    46. char * saveptr;
    47. int i = 0;
    48. /* donot create path */
    49. if (pathDepth == 0) return result;
    50. while ((path[i] == '/') && i < MAX_FILENAME_LEN){
    51. nextDir[i] = path[i];
    52. i++;
    53. }
    54. nextDir[i] = 0;
    55. printf("nextDir: %s\n", nextDir);
    56. strcpy(directory, &path[i]);
    57. printf("Directory: %s\n", directory);
    58. num_parts = 0;
    59. parts[num_parts] = strtok_r(directory, "\\/", &saveptr);
    60. while (parts[num_parts] != NULL){
    61. parts[++num_parts] = strtok_r(NULL, "\\/", &saveptr);
    62. }
    63. // for (i = 0; i < num_parts; i++){
    64. // printf("%d: %s\n", i+1, parts[i]);
    65. //}
    66. if (pathDepth < 0){
    67. pathDepth = num_parts + pathDepth;
    68. if (pathDepth < 1) pathDepth = 1;
    69. }
    70. for (i = 0; i < num_parts && result != false; i++){
    71. strcat(nextDir, parts[i]);
    72. if (i >= pathDepth)
    73. {
    74. // printf("depath = %d,create path %s\n",i, nextDir);
    75. if (mkdir(nextDir, 0777) != 0 && errno != EEXIST){
    76. result = false;
    77. }
    78. }
    79. strcat(nextDir, "/");
    80. }
    81. return result;
    82. }
    83. /* 测试主程序 */
    84. int main(int argc, char ** argv)
    85. {
    86. if (argc != 2){
    87. fprintf(stderr, "Usage: %s pathname\n", argv[0]);
    88. return -1;
    89. }
    90. std::string filePath = argv[1];
    91. std::cout << "Check File Path : " << filePath << " exists?" << std::endl;;
    92. CheckPath * pcp = new CheckPath();
    93. /* Test file path exists or not */
    94. std::cout << "*****************************************"<
    95. std::cout << "Test Function 1 checkPath:" << std::endl;
    96. if (pcp->checkPath(filePath))
    97. {
    98. std::cout << "FilePath:" << filePath <<" does exist" << std::endl;
    99. }
    100. else{
    101. std::cout << "FilePath:" << filePath << " does not exist" << std::endl;
    102. }
    103. std::cout << "*****************************************"<
    104. std::cout << "*****************************************"<
    105. std::cout << "Test Function 2 createFilePath:" << std::endl;
    106. std::cout << "Please enter a path to create:";
    107. std::cin >> filePath;
    108. std::cout << "The path to create :" << filePath << std::endl;
    109. int depth;
    110. std::cout << "Please enter the create depth:";
    111. std::cin >> depth;
    112. printf("Depth: %d\n", depth);
    113. if (pcp->createFilePath(filePath.c_str(), depth)){
    114. std::cout << "Create the " << depth << " depth of file path " << filePath << std::endl;
    115. }
    116. else{
    117. std::cout << "can not create file path " << filePath << std::endl;
    118. }
    119. std::cout << "*****************************************"<
    120. delete pcp;
    121. return 0;
    122. }

    编译以上代码,观察结果:

    1. orangepi@orangepi5:~/C_program$ ./checkpath aa
    2. Check File Path : aa exists?
    3. *****************************************
    4. Test Function 1 checkPath:
    5. FilePath:aa/ does exist
    6. *****************************************
    7. *****************************************
    8. Test Function 2 createFilePath:
    9. Please enter a path to create:/home/orangepi/C_program/aa/bb
    10. The path to create :/home/orangepi/C_program/aa/bb
    11. Please enter the create depth:3
    12. Depth: 3
    13. nextDir: /
    14. Directory: home/orangepi/C_program/aa/bb
    15. Create the 3 depth of file path /home/orangepi/C_program/aa/bb
    16. *****************************************
    17. orangepi@orangepi5:~/C_program$ ls aa/
    18. 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