文件操作类 近来在项目中发现了很多的文件操作,故自己封装了一个文件操作类。实现代码如下: /* FileName: FileOperation.cpp Author: ACb0y Create Time: 2011年3月15日20:15:03 Last Modify Time: 2011年3月15日22:52:37 */ #include <iostream> using namespace std; //文件各种操作的返回值 const int FILE_OPEN_SUCCEED = 1; const int FILE_OPEN_FAILURE = -1; const int FILE_COPY_SUCCEED = 1; const int FILE_COPY_FAILURE = -1; const int FILE_REMOVE_SUCCEED = 1; const int FILE_REMOVE_FAILURE = -1; const int FILE_DEL_SUCCEED = 1; const int FILE_DEL_FAILURE = -1; const int FILE_RENAME_SUCCEED = 1; const int FILE_RENAME_FAILURE = -1; const int MAX_LINE = 1024; class FileOperation { //属性 private: FILE * m_pSrcFile; FILE * m_pDstFile; protected: //nothing. public: //nothing. //构造函数&&析构函数 private: //nothing. protected: //nothing. public: FileOperation(); //服务 private: //nothing. protected: //nothing. public: //删除文件 int delFile(char * pStrFileName); //重命名文件 int renameFile(char * pStrOldFileName, char * pStrNewFileName); //移动文件 int removeFile(char * pStrSrcPath, char * pStrDstPath, char * pStrFileName); //拷贝文件 int copyFile(char * pStrSrcFileName, char * pStrDstFileName); }; /* 函数名:FileOperation(); 功能:构造函数 参数:无 返回值:无 */ FileOperation::FileOperation() { m_pSrcFile = NULL; m_pDstFile = NULL; } /* 函数名:int delFile(char * pStrFileName); 功能:删除指定的文件 参数: 输入: pStrFileName (char *):要删除的文件的文件名 输出:无 返回值: FILE_DEL_SUCCEED: 文件删除成功 FILE_DEL_FAILURE: 文件删除失败 */ int FileOperation::delFile(char * pStrFileName) { if (0 == remove(pStrFileName)) { return FILE_DEL_SUCCEED; } return FILE_DEL_FAILURE; } /* 函数名:int renameFile(char * pStrOldFileName, char * pStrNewFileName); 功能:对指定的文件重命名 参数: 输入: pStrOldFileName (char *): 指定的文件的文件名 pStrNewFileName (char *): 新的文件名 输出:无 返回值: FILE_RENAME_SUCCEED: 文件重命名成功 FILE_RENAME_FAILURE: 文件重命名失败 */ int FileOperation::renameFile(char * pStrOldFileName, char * pStrNewFileName) { if (0 == rename(pStrOldFileName, pStrNewFileName)) { return FILE_RENAME_SUCCEED; } return FILE_RENAME_FAILURE; } /* 函数名:int removeFile(char * pStrSrcPath, char * pStrDstPath, char * pStrFileName); 功能:移动文件 参数: 输入: pStrSrcPath (char *): 文件源路径 pStrDstPath (char *): 文件目的路径 pStrFileName (char *): 文件名 输出:无 返回: FILE_REMOVE_SUCCEED: 移动文件成功 FILE_REMOVE_FAILURE: 移动文件失败 */ int FileOperation::removeFile(char * pStrSrcPath, char * pStrDstPath, char * pStrFileName) { char strSrcPathAndFileName[MAX_LINE]; char strDstPathAndFileName[MAX_LINE]; strcpy(strSrcPathAndFileName, pStrSrcPath); strcat(strSrcPathAndFileName, pStrFileName); strcpy(strDstPathAndFileName, pStrDstPath); strcat(strDstPathAndFileName, pStrFileName); if (0 == rename(strSrcPathAndFileName, strDstPathAndFileName)) { return FILE_REMOVE_SUCCEED; } return FILE_REMOVE_FAILURE; } /* 函数:int copyFile(char * pStrSrcFileName, char * pStrDstFileName); 功能:复制文件 参数: 输入: pStrSrcFileName (char *): 源文件名 pStrDstFileName (char *): 目标文件名 输出:无 返回: */ int FileOperation::copyFile(char * pStrSrcFileName, char * pStrDstFileName) { m_pSrcFile = fopen(pStrSrcFileName, "r"); if (NULL == m_pSrcFile) { return FILE_OPEN_FAILURE; } m_pDstFile = fopen(pStrDstFileName, "w"); if (NULL == m_pDstFile) { return FILE_OPEN_FAILURE; } while (!feof(m_pSrcFile)) { int tmp = getc(m_pSrcFile); if (feof(m_pSrcFile)) { break; } putc(tmp, m_pDstFile); } fclose(m_pSrcFile); fclose(m_pDstFile); m_pSrcFile = NULL; m_pDstFile = NULL; return FILE_COPY_SUCCEED; } int main() { FileOperation fileOperation; if (fileOperation.copyFile("C://a.txt", "D://copy.txt") > 0) { cout << "Copy succeed." << endl; } else { cout << "Coyp failure." << endl; } if (fileOperation.delFile("D://copy.txt") > 0) { cout << "Delete coyp.txt succeeed." << endl; } else { cout << "Delete copy.txt failure." << endl; } if (fileOperation.removeFile("C://", "D://", "a.txt") > 0) { cout << "File remove succeed." << endl; } else { cout << "File remove failure." << endl; } if (fileOperation.renameFile("D://a.txt", "D://copy.txt") > 0) { cout << "Rename File succeed." << endl; } else { cout << "Rename File failure." << endl; } return 0; } 运行结果如下: