跨平台删除文件夹,拷贝文件夹,拷贝文件
2010-06-02 12:06
113 查看
为Windows和Linux平台。
#include <string> #include <algorithm> #include <functional> #include <map> #include <time.h> #include <vector> #include <sstream> #include <fstream> #include <sys/types.h> #include <sys/stat.h> #include <stdio.h> #include <errno.h> #ifdef WIN32 #include <winsock2.h> #include <process.h> #pragma comment(lib, "Ws2_32.lib") #include <direct.h> #include "tlhelp32.h" #include "shellapi.h" #else #include <unistd.h> #include <dirent.h> #include <stdlib.h> #include <sys/procfs.h> #include <unistd.h> #include <stropts.h> #include <fcntl.h> #include <signal.h> #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(); }
相关文章推荐
- 跨平台删除文件夹,拷贝文件夹,拷贝文件
- net8:简易的文件磁盘管理操作二(包括文件以及文件夹的编辑创建删除移动拷贝重命名等)
- Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)
- Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)
- C#文件写入,C#追加文件,C#拷贝文件,C#删除文件 ,C#移动文件 ,C#递归删除文件夹及文件
- C# 文件操作(全部) 追加、拷贝、删除、移动文件、创建目录 修改文件名、文件夹名
- C# 文件操作 全收录 追加、拷贝、删除、移动文件、创建目录、递归删除文件夹及文件....
- C# 文件操作(全部) 追加、拷贝、删除、移动文件、创建目录 修改文件名、文件夹名
- C# 文件操作(全部) 追加、拷贝、删除、移动文件、创建目录 修改文件名、文件夹名
- 文件(文件夹)删除拷贝操作类
- iOS文件和文件夹的创建,删除,移动, 拷贝,是否存在及简单数据类型的读写
- 在拷贝,删除文件时显示飞行的文件夹动画
- [转]C# 文件操作 全收录 追加、拷贝、删除、移动文件、创建目录、递归删除文件夹及文件....
- C# 文件操作 全收录 追加、拷贝、删除、移动文件、创建目录、递归删除文件夹及文件....
- 把devices.xml这个文件删除,再把sdk里面tools\lib下的这个文件拷贝到你删除的那个文件夹里,重启eclipse
- Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)
- 在拷贝、删除文件时显示飞行的文件夹动画
- Android 关于文件及文件夹的创建 、删除、重命名、复制拷贝、添加内容、修改内容、读取内容
- Asp.Net 文件操作基类(读取,删除,批量拷贝,删除,写入,获取文件夹大小,文件属性,遍历目录)
- C#文件写入,C#追加文件,C#拷贝文件,C#删除文件 ,C#移动文件 ,C#递归删除文件夹及文件