开发过程中需要对Linux的文件和目录进行拷贝和删除,各种查找发现C++居然没有相关功能的标准接口,于是就各方查找,希望能够找到某位大佬分享的成熟方案,but并没有找到我想要的。
于是便参考Linux公社的一个帖子关于目录拷贝的实现思路,并在其基础上增加了递归删除的功能,在此贴上代码作为笔记,以备下次使用。
#include#include #include #include #include #include #include #include static bool isFile(const std::string& filename) { struct stat buffer; return (stat (filename.c_str(), &buffer) == 0 && S_ISREG(buffer.st_mode)); } static bool isDirectory(const std::string& filefodler) { struct stat buffer; return (stat (filefodler.c_str(), &buffer) == 0 && S_ISDIR(buffer.st_mode)); } int copyFile(const std::string& old_name, const std::string& new_name) { std::ifstream ifs(old_name, std::ifstream::binary); std::ofstream ofs(new_name, std::ifstream::binary| std::ifstream::trunc); if(ifs.good() == false) { return -1; } ofs << ifs.rdbuf(); ifs.close(); ofs.close(); return 0; } int copyFileAndDirectory(const std::string& old_path, const std::string& new_path) { int result = 0; DIR * p_dir; struct dirent * p_dirent; if(isDirectory(old_path)) { if(access(new_path.c_str(),0) == -1) { if(mkdir(new_path.c_str(), 0755) < 0) { std::cout<< "[copyFileAndDirectory] mkdir failed , the errno = " << strerror(errno) << std::endl; return -1; } } if((p_dir = opendir(old_path.c_str())) == NULL ) { std::cout << "Usage:cp -r error: " << strerror(errno) << std::endl; return -1; } while((p_dirent = readdir(p_dir)) != NULL) { std::string file_name = old_path + "/" + p_dirent->d_name; std::string new_file_name = new_path + "/" + p_dirent->d_name; // It is a directory if(isDirectory(file_name) && (0 != strcmp(p_dirent->d_name, ".")) && (0 != strcmp(p_dirent->d_name, ".."))) { result = copyFileAndDirectory(file_name,new_file_name); if(result < 0) { return result; } } else if((0 != strcmp(p_dirent->d_name, ".")) && (0 != strcmp(p_dirent->d_name, ".."))) { result = copyFile(file_name, new_file_name); if(result < 0) { return result; } } } closedir(p_dir); } return result; } int removeFileAndDirectory(const std::string& path) { int result = 0; DIR * p_dir; struct dirent * p_dirent; if(isDirectory(path)) { if((p_dir = opendir(path.c_str())) == NULL ) { std::cout << "Opendir error: " << strerror(errno) << std::endl; return -1; } while((p_dirent = readdir(p_dir)) != NULL) { std::string file_name = path + "/" + p_dirent->d_name; if(isDirectory(file_name) && (0 != strcmp(p_dirent->d_name, ".")) && (0 != strcmp(p_dirent->d_name, ".."))) { result = removeFileAndDirectory(file_name); if(result < 0) { return result; } } else if((0 != strcmp(p_dirent->d_name, ".")) && (0 != strcmp(p_dirent->d_name, ".."))) { result = remove(file_name.c_str()); if(result < 0) { return result; } } else { } } closedir(p_dir); result = rmdir(path.c_str()); } else if(isFile(path)) { result = remove(path.c_str()); } else { } return result; } int main(void) { std::string src = "/Test/test"; std::string des = "/Test/test2"; int result = copyDirectory(src, des); std::cout<< result << std::endl; result = removeFileAndDirectory(des); std::cout<< result << std::endl; return 0; }



