typedef struct _CSiteNameInfo
{
TCHAR SiteID[4];//序号
TCHAR SiteName[18];//工地名
}CSiteNameInfo;
CFile mFile;
CSiteNameInfo siteNameData;
1.文件的创建与打开
//如果文件存在就打开,不存在则创建
if (!mFile.Open(L"\\My Documents\\MyFile",CFile::modeCreate|CFile::modeWrite))//创建失败
{
AfxMessageBox(_T("创建数据库失败"));
return;
}
2.关闭文件
mFile.Close();
3.写文件(以结构体为单元)
wcscpy_s(siteNameData.SiteID, L"site" );
wcscpy_s(siteNameData.SiteName,L"name");
mFile.Open(L"\\My Documents\\MyFile",CFile::modeCreate|CFile::modeWrite);
mFile.SeekToEnd();//将指针移到文件末尾
mFile.Write(&siteNameData,sizeof(siteNameData));
mFile.Close();
4.读文件(以结构体为单元)
mFile.Read(&siteNameData,sizeof(siteNameData));
例:读取整个文件并将其存入数组
CSiteNameInfo siteInfo[10];
for(int i = 0; i < 10; i++)
{
mFile.Read(&siteInfo[i],sizeof(siteInfo[i]));
}
//关闭数据文件
mFile.Close();
5.查询文件是否存在
if(::GetFileAttributes(L"\\My Documents\\MyFile") != 0xFFFFFFFF)
{
::AfxMessageBox(_T("此文件已存在!"));
}
else
{
::AfxMessageBox(_T("此文件不存在!"));
}
6.新建目录
if (!CreateDirectory(L"\\My Documents\\mine" , NULL))
{
AfxMessageBox(_T("创建目录失败"));
return FALSE;
}
7.复制文件内容到另一个新文件
if(!CopyFile(L"\\My Documents\\old.dat",
L"\\My Documents\\new.dat",TRUE))//将old.dat文件的信息复制到new.dat文件内
{
MessageBox(L"复制失败!");
return;
}
8.删除文件
DeleteFile(L"\\My Documents\\MyFile.dat");
9.删除目录
if (!RemoveDirectory(L"\\My Documents\\mine" ))//删除目录
{
MessageBox(L"删除目录失败!");
return;
}
以下部分来自CSDN博客,转载请标明出处:http://blog.csdn.net/armeasy/archive/2010/07/12/5729625.aspx
10.在WinCE下实现将某文件夹下的所有文件(包括文件夹)拷贝到另一个文件夹中.
算法不复杂,简单实用.(经试验,此种方法效果不理想)
//szExistingDir:源文件夹
//szNewDir:目标文件夹
//注意:目标文件夹必须要存在,否则该函数将返回FALSE.
BOOL BrowseAndCopy(const CString szExistingDir, const CString szNewDir)
{
CString szExistDir;
CString szAimDir=szNewDir; //保存目标文件夹路径
CString szFindDir=szExistingDir; //保存源文件夹路径
if(szFindDir.Right(1)!="\\")
{
szFindDir+="\\";
szExistDir=szFindDir;
}
szFindDir+="*.*"; //搜索所有文件
WIN32_FIND_DATA fd;
HANDLE hFind;
hFind=FindFirstFile(szFindDir,&fd); //寻找第一个文件
if(hFind!=INVALID_HANDLE_VALUE)
{
do{
if(fd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY) //判断是否文件夹
{
if(szAimDir.Right(1)!="\\")
{
szAimDir+="\\";
}
CreateDirectory(szAimDir+fd.cFileName,NULL); //在目标文件夹中创建相应的子文件夹
BrowseAndCopy(szExistDir+fd.cFileName,szAimDir+fd.cFileName); //采用递归查找子文件下的文件
}
else
{
if(szAimDir.Right(1)!="\\")
{
szAimDir+="\\";
}
if(CopyFile(szExistDir+fd.cFileName,szAimDir+fd.cFileName,FALSE)==FALSE) //拷贝文件到目标文件夹
{
return FALSE;
}
}
}while(FindNextFile(hFind,&fd)); //查找是否存在下一个文件
}
else
{
//源文件夹为空,返回
return FALSE;
} return TRUE;
}
11:vc中递归删除非空文件夹
/
// 函数类型: RemoveFileFolder
// 返 回 值: null
// 功能描述: 递归删除文件夹及内部文件
// 函数作者:
// 创建日期:
// 参数列表:
// 变量名: strFileFolderName 变量类型: CString 变量说明: 文件夹名
BOOL RemoveFileFolder(CString strFileFolderName)
{
CFileFind finder;
// 打开指定的文件夹进行搜索
BOOL bWorking = finder.FindFile(strFileFolderName + _T("\\") + _T("*.*"));
while(bWorking)
{
// 从当前目录搜索文件
bWorking = finder.FindNextFile();
CString strFileName = finder.GetFileName();
CString strSrc = strFileFolderName + L"\\" + strFileName;
// 判断搜索到的是不是"."和".."目录
if(!finder.IsDots())
{
// 判断搜索到的目录是否是文件夹
if(finder.IsDirectory())
{
if(!RemoveFileFolder(strSrc)) // 如果是文件夹的话,进行递归
{
return false;
}
}
else
{
if(!DeleteFile(strSrc))// 如果是文件,进行删除
{
return false;
}
}
}
}
finder.Close();
if(!RemoveDirectory(strFileFolderName)) //删除空目录
{
MessageBox(L"删除目录失败!");
return FALSE;
}
return true;
}
12.vc中复制文件夹到另一个文件夹
/
// 函数类型: CopyFileFolder
// 返 回 值: null
// 功能描述: 复制一个文件夹内的文件到另一个文件夹
// 函数作者:
// 创建日期:
// 参数列表:
// 变 量 名: strFromFile 变量类型: CString 变量说明:源文件名
// 变 量 名: strToFile 变量类型: CString 变量说明:要保存到的文件名
BOOL CopyFileFolder(CString strSrcPath, CString strDstPath)
{ // 创建目标文件夹
CreateDirectory(strDstPath,NULL);
CFileFind finder;
// 打开指定的文件夹进行搜索
BOOL bWorking = finder.FindFile(strSrcPath + _T("\\") + _T("*.*"));
while(bWorking)
{
// 从当前目录搜索文件
bWorking = finder.FindNextFile();
CString strFileName = finder.GetFileName();
CString strSrc = strSrcPath + L"\\" + strFileName;
CString strDst = strDstPath + L"\\" + strFileName;
// 判断搜索到的是不是"."和".."目录
if(!finder.IsDots())
{
// 判断搜索到的目录是否是文件夹
if(finder.IsDirectory())
{
// 如果是文件夹的话,进行递归
if(!CopyFileFolder(strSrc, strDst))
{
return false;
}
}
else
{
// 如果是文件,进行复制
if(!CopyFile(strSrc, strDst, FALSE))
{
return false;
}
}
}
}
return true;
}