VC文件操作总结
2010-06-22 11:03
295 查看
1.读写文件
CString strPath=“c://test.txt”;//文件路径
CFile File;
if (File.Open(strPath,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite))//写文件
{
//do something ...
File.Close();
}
CString strPath=“c://test.txt”;
CFile File;
if (File.Open(strPath,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeRead))//读文件
{
//do something ...
File.Close();
}
CString strPath=“c://test.txt”;
CFile File;
if (File.Open(strPath,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeReadWrite))//读写文件
{
//do something ...
File.Close();
}
2.重命名文件
//strOldPath,strNewPath分别代表旧的文件和新的文件的绝对路径
BOOL renameFile(CString strOldPath,CString strNewPath)
{
try
CFile::Rename(strOldPath, strNewPath);
catch(CFileException* pEx )
{
#ifdef _DEBUG
afxDump << "文件" << strOldPath << "未发现"<<"/n";
#endif
pEx->Delete();
return FALSE;
}
return TRUE;
}
3.重命名文件夹/文件
BOOL MoveFile(
LPCTSTR lpExistingFileName, // 旧文件夹名称
LPCTSTR lpNewFileName // 新文件夹名称
);
或者
BOOL MoveFileEx (
LPCTSTR lpExistingFileName, //旧文件夹名称
LPCTSTR lpNewFileName, //新文件夹名称
DWORD dwFlags
);
或者如下函数
BOOL ReNameFolder(LPCTSTR lpszFromPath,LPCTSTR lpszToPath)
{
int nLengthFrm = strlen(lpszFromPath);
char *NewPathFrm = new char[nLengthFrm+2];
strcpy(NewPathFrm,lpszFromPath);
NewPathFrm[nLengthFrm] = '/0';
NewPathFrm[nLengthFrm+1] = '/0';
SHFILEOPSTRUCT FileOp;
ZeroMemory((void*)&FileOp,sizeof(SHFILEOPSTRUCT));
FileOp.fFlags = FOF_NOCONFIRMATION ;
FileOp.hNameMappings = NULL;
FileOp.hwnd = NULL;
FileOp.lpszProgressTitle = NULL;
FileOp.pFrom = NewPathFrm;
FileOp.pTo = lpszToPath;
FileOp.wFunc = FO_RENAME;
if (SHFileOperation(&FileOp) == 0)
{
delete NewPathFrm;
return TRUE;
}
else
{
delete NewPathFrm;
return FALSE;
}
}
4.查找目录是否存在
//strPath表示要查找的目录路径
BOOL FolderExist(CString strPath)
{
WIN32_FIND_DATA wfd;
BOOL rValue = FALSE;
HANDLE hFind = FindFirstFile(strPath, &wfd);
if ((hFind != INVALID_HANDLE_VALUE) && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
rValue = TRUE;
FindClose(hFind);
return rValue;
}
5.创建目录
BOOL CreateDirectory(
LPCTSTR lpPathName, //创建的目录路径
LPSECURITY_ATTRIBUTES lpSecurityAttributes //创建的目录安全属性
);
返回值:
成功返回非0,失败返回0。
调用GetLastError获得额外的信息.
6.删除目录
方法1:递归调用
//删除文件夹目录(非空)
BOOL DeleteDirectory(char* sDirName)
{
CFileFind tempFind;
char sTempFileFind[MAX_PATH] ;
sprintf(sTempFileFind,"%s//*.*",sDirName);
BOOL IsFinded = tempFind.FindFile(sTempFileFind);
while (IsFinded)
{
IsFinded = tempFind.FindNextFile();
if (!tempFind.IsDots())
{
char sFoundFileName[MAX_PATH];
strcpy(sFoundFileName,tempFind.GetFileName().GetBuffer(MAX_PATH));
if (tempFind.IsDirectory())
{
char sTempDir[MAX_PATH];
sprintf(sTempDir,"%s//%s",sDirName,sFoundFileName);
DeleteDirectory(sTempDir);
}
else
{
char sTempFileName[MAX_PATH];
sprintf(sTempFileName,"%s//%s",sDirName,sFoundFileName);
DeleteFile(sTempFileName);
}
}
}
tempFind.Close();
if(!RemoveDirectory(sDirName))
return FALSE;
return TRUE;
}
方法2:shell API
BOOL DelDirectory(LPCTSTR lpszPath)
{
SHFILEOPSTRUCT FileOp;
FileOp.fFlags = FOF_NOCONFIRMATION;
FileOp.hNameMappings = NULL;
FileOp.hwnd = NULL;
FileOp.lpszProgressTitle = NULL;
FileOp.pFrom = lpszPath;
FileOp.pTo = NULL;
FileOp.wFunc = FO_DELETE;
return SHFileOperation(&FileOp) == 0;
}
CString strPath=“c://test.txt”;//文件路径
CFile File;
if (File.Open(strPath,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite))//写文件
{
//do something ...
File.Close();
}
CString strPath=“c://test.txt”;
CFile File;
if (File.Open(strPath,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeRead))//读文件
{
//do something ...
File.Close();
}
CString strPath=“c://test.txt”;
CFile File;
if (File.Open(strPath,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeReadWrite))//读写文件
{
//do something ...
File.Close();
}
2.重命名文件
//strOldPath,strNewPath分别代表旧的文件和新的文件的绝对路径
BOOL renameFile(CString strOldPath,CString strNewPath)
{
try
CFile::Rename(strOldPath, strNewPath);
catch(CFileException* pEx )
{
#ifdef _DEBUG
afxDump << "文件" << strOldPath << "未发现"<<"/n";
#endif
pEx->Delete();
return FALSE;
}
return TRUE;
}
3.重命名文件夹/文件
BOOL MoveFile(
LPCTSTR lpExistingFileName, // 旧文件夹名称
LPCTSTR lpNewFileName // 新文件夹名称
);
或者
BOOL MoveFileEx (
LPCTSTR lpExistingFileName, //旧文件夹名称
LPCTSTR lpNewFileName, //新文件夹名称
DWORD dwFlags
);
或者如下函数
BOOL ReNameFolder(LPCTSTR lpszFromPath,LPCTSTR lpszToPath)
{
int nLengthFrm = strlen(lpszFromPath);
char *NewPathFrm = new char[nLengthFrm+2];
strcpy(NewPathFrm,lpszFromPath);
NewPathFrm[nLengthFrm] = '/0';
NewPathFrm[nLengthFrm+1] = '/0';
SHFILEOPSTRUCT FileOp;
ZeroMemory((void*)&FileOp,sizeof(SHFILEOPSTRUCT));
FileOp.fFlags = FOF_NOCONFIRMATION ;
FileOp.hNameMappings = NULL;
FileOp.hwnd = NULL;
FileOp.lpszProgressTitle = NULL;
FileOp.pFrom = NewPathFrm;
FileOp.pTo = lpszToPath;
FileOp.wFunc = FO_RENAME;
if (SHFileOperation(&FileOp) == 0)
{
delete NewPathFrm;
return TRUE;
}
else
{
delete NewPathFrm;
return FALSE;
}
}
4.查找目录是否存在
//strPath表示要查找的目录路径
BOOL FolderExist(CString strPath)
{
WIN32_FIND_DATA wfd;
BOOL rValue = FALSE;
HANDLE hFind = FindFirstFile(strPath, &wfd);
if ((hFind != INVALID_HANDLE_VALUE) && (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
rValue = TRUE;
FindClose(hFind);
return rValue;
}
5.创建目录
BOOL CreateDirectory(
LPCTSTR lpPathName, //创建的目录路径
LPSECURITY_ATTRIBUTES lpSecurityAttributes //创建的目录安全属性
);
返回值:
成功返回非0,失败返回0。
调用GetLastError获得额外的信息.
Return code | Description |
---|---|
ERROR_ALREADY_EXISTS | 指定的目录已经存在. |
ERROR_PATH_NOT_FOUND | 如果中间有目录不存在将出错。想直接创建所有路径目录用 SHCreateDirectoryEx 函数. |
方法1:递归调用
//删除文件夹目录(非空)
BOOL DeleteDirectory(char* sDirName)
{
CFileFind tempFind;
char sTempFileFind[MAX_PATH] ;
sprintf(sTempFileFind,"%s//*.*",sDirName);
BOOL IsFinded = tempFind.FindFile(sTempFileFind);
while (IsFinded)
{
IsFinded = tempFind.FindNextFile();
if (!tempFind.IsDots())
{
char sFoundFileName[MAX_PATH];
strcpy(sFoundFileName,tempFind.GetFileName().GetBuffer(MAX_PATH));
if (tempFind.IsDirectory())
{
char sTempDir[MAX_PATH];
sprintf(sTempDir,"%s//%s",sDirName,sFoundFileName);
DeleteDirectory(sTempDir);
}
else
{
char sTempFileName[MAX_PATH];
sprintf(sTempFileName,"%s//%s",sDirName,sFoundFileName);
DeleteFile(sTempFileName);
}
}
}
tempFind.Close();
if(!RemoveDirectory(sDirName))
return FALSE;
return TRUE;
}
方法2:shell API
BOOL DelDirectory(LPCTSTR lpszPath)
{
SHFILEOPSTRUCT FileOp;
FileOp.fFlags = FOF_NOCONFIRMATION;
FileOp.hNameMappings = NULL;
FileOp.hwnd = NULL;
FileOp.lpszProgressTitle = NULL;
FileOp.pFrom = lpszPath;
FileOp.pTo = NULL;
FileOp.wFunc = FO_DELETE;
return SHFileOperation(&FileOp) == 0;
}
相关文章推荐
- VC操作INI文件 ,INI文件操作总结,如何操作INI文件,INI文件使用方法小结
- VC文件读写操作总结
- VC++文件操作之最全篇(总结一下希望对大家有用)
- VC操作INI文件 ,INI文件操作总结,如何操作INI文件,INI文件使用方法小结
- VC文件读写操作总结
- VC文件读写操作总结
- VC文件读写操作总结
- VC文件读写操作总结
- VC 文件操作
- Android - 文件读写操作 总结
- [转]Android - 文件读写操作 总结
- VC(C)操作文件之-文件的打开(fopen函数)
- VC的文件操作
- Android - 文件读写操作 总结
- C/C++ 文件读写操作总结(2)
- .NET对文件与文件夹的操作简单总结
- VC中的文件操作(CFILE)
- 文件读写操作总结
- 项目总结(一)------Python对文件和目录进行操作 (file对象/os/os.path/shutil 模块)
- CentOS常用的文件操作命令总结