跨平台删除文件夹,拷贝文件夹,拷贝文件

为Windows和Linux平台。

 

#include #include #include #include #include #include #include #include #include #include #include #include #ifdef WIN32 #include #include #pragma comment(lib, "Ws2_32.lib") #include #include "tlhelp32.h" #include "shellapi.h" #else #include #include #include #include #include #include #include #include #endif #ifdef WIN32 #define rmdir(x) _rmdir(x) #define close_file(x) _close(x) #define PID DWORD #define mkdir(x) _mkdir(x) #define sp_sleep(x) Sleep(x) #define setWorkingPath(x) SetCurrentDirectory(x) #define getErrorNo() GetLastError() #else #define rmdir(x) rmdir(x) #define close_file(x) close(x) #define PID pid_t #define mkdir(x) mkdir(x, 0) #define sp_sleep(x) usleep(x * 1000) #define setWorkingPath(x) chdir(x) #define getErrorNo() errno #endif /** * @brief delDirectory * * Detailed description. * @param[in] filePath * @return bool */ inline bool delDirectory(std::string filePath) { #ifdef WIN32 std::string tmpStr = "rd /"" + filePath + "/" /s /q"; #else std::string tmpStr = "rm -rf /"" + filePath + "/""; #endif int rt = system(tmpStr.c_str()); if (0 == rt) { return true; } else { return false; } } /** * @brief copyDirectory * * Detailed description. * @param[in] sourcePath * @param[in] destPath * @return bool */ inline bool copyDirectory(std::string sourcePath, std::string destPath) { if (sourcePath == destPath) { return true; } #ifdef WIN32 if ((sourcePath.length() > 0) && (sourcePath[sourcePath.length() - 1] == '/')) { sourcePath = sourcePath.substr(0, sourcePath.length() - 1); } if ((destPath.length() > 0) && (destPath[destPath.length() - 1] == '/')) { destPath = destPath.substr(0, destPath.length() - 1); } std::string tmpStr = "xcopy /"" + sourcePath + "/" /"" + destPath + "/" /e /y /h /c /k /r"; #else std::string tmpStr = "cp -rf /"" + sourcePath + "/" /"" + destPath + "/""; #endif int rt = system(tmpStr.c_str()); if (0 == rt) { return true; } else { return false; } } /** * @brief copyFile * * Detailed description. * @param[in] sourcePath * @param[in] destPath */ inline void copyFile(std::string sourcePath, std::string destPath) { if ((sourcePath.length() > 0) && (sourcePath[sourcePath.length() - 1] == '/')) { sourcePath = sourcePath.substr(0, sourcePath.length() - 1); } if ((destPath.length() > 0) && (destPath[destPath.length() - 1] == '/')) { destPath = destPath.substr(0, destPath.length() - 1); } std::ifstream input(sourcePath.c_str(), std::ios::binary); std::ofstream output(destPath.c_str(), std::ios::binary); output << input.rdbuf(); output.close(); input.close(); }

你可能感兴趣的:(C++,Windows,Linux)