在C++文件操作中,有许多常用操作,本文从文件是否存在,文件夹是否存在,创建文件夹,及重命名文件等入手,提供相应的模块,供读者参考:
#include
#include
#include
#include
#include
#include
#include
#include
//目录是否存在
static bool FolderExist(std::string folderName)
{
//0.参数检测
if (folderName.empty())
{
return false;
}
//1.文件是否存在
if(_access(folderName.c_str(),0))
{
return false;
}
else
{
return true;
}
}
//文件是否存在
static bool FileExist(std::string filename)
{
//0.参数检测
if (filename.empty())
{
return false;
}
//1.文件是否存在
if(_access(filename.c_str(),0))
{
return false;
}
else
{
return true;
}
}
//重命名文件
static bool RenameFile(std::string filenameOld,std::string filenameNew)
{
//0.参数检测
if (filenameOld.empty() || filenameNew.empty())
{
return false;
}
//1.文件是否存在
if (!FileExist(filenameOld))
{
return false;
}
//2.文件重命名
if (0==rename(filenameOld.c_str(),filenameNew.c_str()))
{
return true;
}
else
{
return false;
}
}
//创建目录
static bool CreateFolder(std::string folderName,std::string parentFolderName)
{
//0.参数检测
if (folderName.empty() || parentFolderName.empty())
{
return false;
}
//1.创建子文件夹
std::string newFolderName=parentFolderName+'\\'+folderName;
if(_access(newFolderName.c_str(),6)==-1)
{
if (0==_mkdir(newFolderName.c_str()))
{
return true;
}
else
{
if (errno==EEXIST)
{
return true;
}
else
{
return false;
}
}
}
else
return false;
}
//删除目录及内容
bool FileAndFolderOperate::DeleteFolder(std::string folderName)
{
//0.参数检测
if (folderName.empty())
{
return false;
}
//1.删除目录及内容
std::string strDirName = folderName;
BOOL result;
HANDLE Handle;
WIN32_FIND_DATA fData;
std::string strTemp;
DWORD errorcode;
std::string findfilter=folderName + "\\*.*";
Handle = FindFirstFile(findfilter.c_str(), &fData);
//2.查找失败则返回
if (Handle == INVALID_HANDLE_VALUE)
return FALSE;
//3.循环迭代查找子文件夹和文件
do {
errorcode = GetLastError();
if (fData.cFileName[0] == '.')
continue;
if (fData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) {
if (strDirName[strDirName.length()-1] != '\\'){
TCHAR chA[MAX_PATH];
std::string strA = strDirName+'\\'+ fData.cFileName;
lstrcpy(chA, strA.c_str());
DeleteFolder(chA);
}
else{
TCHAR chB[MAX_PATH];
std::string strB = strDirName + fData.cFileName;
lstrcpy(chB, strB.c_str());
DeleteFolder(chB);
}
strTemp = strDirName + "\\" + fData.cFileName;
SetFileAttributes(strTemp.c_str(), ~FILE_ATTRIBUTE_READONLY);
if (!RemoveDirectory(strTemp.c_str()))
result = FALSE;
else
result = TRUE;
}
else
{
strTemp = strDirName + "\\" + fData.cFileName;
BOOL bl = SetFileAttributes(strTemp.c_str(), ~FILE_ATTRIBUTE_READONLY);
if (!DeleteFile(strTemp.c_str()))
result = FALSE;
else
result = TRUE;
}
}while(FindNextFile(Handle,&fData));
errorcode = GetLastError();
if (errorcode == ERROR_NO_MORE_FILES)//空目录
{
::RemoveDirectory(strDirName.c_str());
result = TRUE;
}
if (Handle)
FindClose(Handle);
return result;
}
//选择工作文件夹
static bool SelectFolderPath(std::string& folderName)
{
TCHAR szBuffer[MAX_PATH] = {0};
BROWSEINFO bi;
ZeroMemory(&bi,sizeof(BROWSEINFO));
bi.hwndOwner =NULL;/*GetForegroundWindow()*/
bi.pszDisplayName = szBuffer;
bi.pidlRoot=NULL;
bi.lpszTitle = _T("从下面选文件夹目录:");
bi.ulFlags = BIF_NEWDIALOGSTYLE;;
LPITEMIDLIST idl = SHBrowseForFolder(&bi);
if (NULL == idl)
{
return false;
}
SHGetPathFromIDList(idl,szBuffer);
folderName=szBuffer;
return true;
}
代码能够提高工作效率。