收藏 分享(赏)

C++文件操作.doc

上传人:kpmy5893 文档编号:9364855 上传时间:2019-08-03 格式:DOC 页数:22 大小:131.50KB
下载 相关 举报
C++文件操作.doc_第1页
第1页 / 共22页
C++文件操作.doc_第2页
第2页 / 共22页
C++文件操作.doc_第3页
第3页 / 共22页
C++文件操作.doc_第4页
第4页 / 共22页
C++文件操作.doc_第5页
第5页 / 共22页
点击查看更多>>
资源描述

1、SHGetSpecialFolderPath获取特殊目录MFC文件操作大全1.创建文件夹 CreateDirectory(%1,NULL); 2.创建文件 CFile file; file.Open(%1,CFile:modeCreate|CFile:modeWrite); 3.删除文件 DeleteFile(%1); 4.删除文件夹 RemoveDirectory(%1); 5.删除一个目录下所有的文件夹 CFileFind finder; BOOL bWorking = finder.FindFile(%1+“/*.*“); while (bWorking) bWorking = find

2、er.FindNextFile(); if (finder.IsDirectory() RemoveDirectory(finder.GetFilePath(); 6.清空文件夹 RemoveDirectory(%1); CreateDirectory(%1,NULL); 7.读取文件 char sRead1024; CFile mFile(_T(%1),CFile:modeRead); while (sRead!=null) mFile.Read(sRead,1024); CString %2(sRead); %3 作者:无尾兽零号机 4位粉丝 2009-10-23 22:13 回复此发言2

3、 回复:MFC 文件操作大全8.写入文件 CFile mFile(_T(%1), CFile:modeWrite|CFile:modeCreate); mFile.Write(%2,sizeof(%2); mFile.Flush(); mFile.Close(); 9.写入随机文件 char szTempPath_MAX_PATH,szTempfile_MAX_PATH; GetTempPath(_MAX_PATH, szTempPath); GetTempFileName(szTempPath,_T (“my_“),0,szTempfile); CFile m_tempFile(szTemp

4、file,CFile: modeCreate|CFile: modeWrite); char m_char=a; m_tempFile.Write( m_tempFile.Close(); /循环写入多个值 strTempA; int i; int nCount=6; /共有 6个文件名需要保存 for (i=0;istrTemp.Format(“%d“,i); strTempA=文件名; /文件名可以从数组,列表框等处取得. :WritePrivateProfileString(“UseFileName“,“FileName“+strTemp,strTempA, c:/usefile/use

5、file.ini); strTemp.Format(“%d“,nCount); :WritePrivateProfileString(“FileCount“,“Count“,strTemp,“c:/usefile/usefile.ini“); /将文件总数写入,以便读出. /读出 nCount=:GetPrivateProfileInt(“FileCount“,“Count“,0,“c:/usefile/usefile.ini“); for(i=0;istrTemp.Format(“%d“,i); strTemp=“FileName“+strTemp; :GetPrivateProfileSt

6、ring(“CurrentIni“,strTemp,“default.fil“, strTempA.GetBuffer(MAX_PATH),MAX_PATH,“c:/usefile/usefile.ini“);/使用 strTempA中的内容. 10.读取文件属性 dwAttrs = GetFileAttributes(%1); if (dwAttrs 12.枚举一个目录下所有文件夹 CFileFind finder; BOOL bWorking = finder.FindFile(%1+“/*.*“); while (bWorking) bWorking = finder.FindNextF

7、ile(); if(finder.IsDirectory() CString %1=finder.GetFilePath(); %2 13.复制文件夹 WIN32_FIND_DATA FileData; HANDLE hSearch; DWORD dwAttrs; char szDirPath = %2; char szNewPathMAX_PATH; char szHomeMAX_PATH; BOOL fFinished = FALSE; if (!CreateDirectory(szDirPath, NULL) /不能创建新的目录 return; hSearch = FindFirstFi

8、le(%1+“/*.*“, if (hSearch = INVALID_HANDLE_VALUE) return; while (!fFinished) 作者:无尾兽零号机 4位粉丝 2009-10-23 22:14 回复此发言3 回复:MFC 文件操作大全lstrcpy(szNewPath, szDirPath); lstrcat(szNewPath, FileData.cFileName); if (CopyFile(FileData.cFileName, szNewPath, FALSE) dwAttrs = GetFileAttributes(FileData.cFileName);

9、if (!(dwAttrs else /不能复制文件 return; if (!FindNextFile(hSearch, else /找不到下一个文件 return; FindClose(hSearch); 14.复制一个目录下所有的文件夹到另一个文件夹下 WIN32_FIND_DATA FileData; HANDLE hSearch; DWORD dwAttrs; char szDirPath = %2; char szNewPathMAX_PATH; char szHomeMAX_PATH; BOOL fFinished = FALSE; if (!CreateDirectory(sz

10、DirPath,NULL) /不能创建新的目录 return; BOOL bWorking = finder.FindFile(%1+“/*.*“); while (bWorking) bWorking = finder.FindNextFile(); if(finder.IsDirectory() hSearch = FindFirstFile(finder.GetFilePath()+“/*.*“, if (hSearch = INVALID_HANDLE_VALUE) return; while (!fFinished) lstrcpy(szNewPath, szDirPath); ls

11、trcat(szNewPath, FileData.cFileName); if (CopyFile(FileData.cFileName, szNewPath, FALSE) dwAttrs = GetFileAttributes(FileData.cFileName); if (!(dwAttrs else /不能复制文件 return; if (!FindNextFile(hSearch, else /找不到下一个文件 return; FindClose(hSearch); 15.移动文件夹 WIN32_FIND_DATA FileData; HANDLE hSearch; DWORD

12、dwAttrs; char szDirPath = %2; char szNewPathMAX_PATH; char szHomeMAX_PATH; BOOL fFinished = FALSE; if (!CreateDirectory(szDirPath, NULL) /不能创建新的目录 return; hSearch = FindFirstFile(%1+“/*.*“, if (hSearch = INVALID_HANDLE_VALUE) return; while (!fFinished) lstrcpy(szNewPath, szDirPath); lstrcat(szNewPat

13、h, FileData.cFileName); if (CopyFile(FileData.cFileName, szNewPath, FALSE) dwAttrs = GetFileAttributes(FileData.cFileName); if (!(dwAttrs else /不能复制文件 return; if (!FindNextFile(hSearch, else /找不到下一个文件 return; FindClose(hSearch); RemoveDirectory(%1); 作者:无尾兽零号机 4位粉丝 2009-10-23 22:14 回复此发言4 回复:MFC 文件操作

14、大全16.移动一个文件夹下所有的文件夹到另一个目录下 WIN32_FIND_DATA FileData; HANDLE hSearch; DWORD dwAttrs; char szDirPath = %2; char szNewPathMAX_PATH; char szHomeMAX_PATH; BOOL fFinished = FALSE; if (!CreateDirectory(szDirPath,NULL) /不能创建新的目录 return; BOOL bWorking = finder.FindFile(%1+“/*.*“); while (bWorking) bWorking =

15、 finder.FindNextFile(); if(finder.IsDirectory() hSearch = FindFirstFile(finder.GetFilePath()+“/*.*“, if (hSearch = INVALID_HANDLE_VALUE) return; while (!fFinished) lstrcpy(szNewPath, szDirPath); lstrcat(szNewPath, FileData.cFileName); if (CopyFile(FileData.cFileName, szNewPath, FALSE) dwAttrs = GetF

16、ileAttributes(FileData.cFileName); if (!(dwAttrs else /不能复制文件 return; if (!FindNextFile(hSearch, else /找不到下一个文件 return; FindClose(hSearch); RemoveDirectory(finder.GetFilePath().GetBuffer(0); 17.以一个文件夹的框架在另一个目录创建文件夹和空文件 WIN32_FIND_DATA FileData; HANDLE hSearch; DWORD dwAttrs; char szDirPath = %2; cha

17、r szNewPathMAX_PATH; char szHomeMAX_PATH; BOOL fFinished = FALSE; if (!CreateDirectory(szDirPath, NULL) /不能创建新的目录 return; hSearch = FindFirstFile(%1+“/*.*“, if (hSearch = INVALID_HANDLE_VALUE) return; while (!fFinished) lstrcpy(szNewPath, szDirPath); lstrcat(szNewPath, FileData.cFileName); HANDLE hF

18、ile=CreateFileHandle hFile=CreateFile(szNewPath,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN,NULL);if(!hFile) /不能创建文件 return; if (!FindNextFile(hSearch, else /找不到下一个文件 return; FindClose(hSearch); 18.复制文件 CopyFile(%1,%2,true); 作者:无尾兽零号机 4位粉丝 2009-10-

19、24 15:17 回复此发言5 回复:MFC 文件操作大全19.复制一个文件夹下所有的文件到另一个目录 /#include using std:string; char sep=/; #ifdef _WIN32 sep=/; #endif CFileFind finder; BOOL bWorking = finder.FindFile(%1+“/*.*“); while (bWorking) bWorking = finder.FindNextFile(); if(!finder.IsDirectory() | finder.IsDots() string s(finder.GetFileN

20、ame(); CString sourcefile(%1); if(s.rfind(sep,s.length()!=string:npos) sourcefile=sourcefile+“/“+s.substr(i+1,s.length()-i); CString targetfile(s.substr(i+1,s.length()-i); targetfile=%2+“/“+targetfile/; CopyFile(sourcefile.GetBuffer(0),targetfile.GetBuffer(0),true); 20.提取扩展名 CString path(%1); CStrin

21、g %2=path.Mid(path.ReverseFind(.); 21.提取文件名 CString path(%1); CString %2=path.Mid(path.ReverseFind(/)+1); 22.提取文件路径 char appNameMAX_PATH; GetModualFileName(NULL,appName,MAX_PATH); 作者:无尾兽零号机 4位粉丝 2009-10-24 15:17 回复此发言6 回复:MFC 文件操作大全23.替换扩展名 /#include using std:string; string s(%1); string newExt(%2)

22、; string:size_type i=s.rfind(.,s.length(); if(i!=string:npos) s.replace(i+1,newExt.length(),newExt); CString %3(s); 24.追加路径 /* #include #include #include #include */ using namespace std; using namespace boost:filesystem; try path p1=complete(path(%2,native), path(%1,native); path p2=system_complete(

23、path(%2,native); CString %3(p3); catch(exception 25.移动文件 MoveFile(%1,%2); 作者:无尾兽零号机 4位粉丝 2009-10-24 15:17 回复此发言7 回复:MFC 文件操作大全26.移动一个文件夹下所有文件到另一个目录 /#include using std:string; char sep=/; #ifdef _WIN32 sep=/; #endif CFileFind finder; BOOL bWorking = finder.FindFile(%1+“/*.*“); while (bWorking) bWork

24、ing = finder.FindNextFile(); if(!finder.IsDirectory() | finder.IsDots() string s(finder.GetFileName(); CString sourcefile(%1); if(s.rfind(sep,s.length()!=string:npos) sourcefile=sourcefile+“/“+s.substr(i+1,s.length()-i); CString targetfile(s.substr(i+1,s.length()-i); targetfile=%2+“/“+targetfile/; M

25、oveFile(sourcefile.GetBuffer(0),targetfile.GetBuffer(0),true); 27.指定目录下搜索文件 CString strFileTitle; CFileFind finder; BOOL bWorking = finder.FindFile (“C:/windows/sysbkup/*.cab“); while(bWorking) bWorking=finder.FindNextFile(); strFileTitle=finder.GetFileTitle(); 作者:无尾兽零号机 4位粉丝 2009-10-25 10:44 回复此发言8

26、 回复:MFC 文件操作大全28.打开对话框 CFileDialog mFileDlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT,“All Files (*.*)|*.*|“,AfxGetMainWnd();CString str(“ “,10000); mFileDlg.m_ofn.lpstrFile=str.GetBuffer(10000); str.ReleaseBuffer(); POSITION mPos=mFileDlg.GetStartPosition(); CString p

27、athName(“ “,128); CFileStatus status; while(mPos!=NULL) pathName=mFileDlg.GetNextPathName(mPos); CFile:GetStatus( pathName, status ); 29.文件分割 CFile m_File; CString m_Filename,m_FileTitle,m_FilePath; m_FileName=%1; char pBuf4096; if(m_File.Open(m_FileName,CFile:modeRead | CFile:shareDenyWrite) m_File

28、Name=m_File.GetPathName(); m_FileTitle=m_File.GetFileTitle(); DWORD FileLength=m_File.GetLength(); DWORD PartLength=FileLength/2+FileLength%2; int nCount=1; CString strName; CFile wrFile; DWORD ReadBytes; while(true) ReadBytes=m_File.Read(pBuf,PartLength); strName.Format(“%s%d“,m_FIleTitle,nCount);

29、wrFile.Open(strName,CFile:modeWrite | CFile:modeCreate); wrFile.Write(pBuf,ReadBytes); wrFile.Close(); if(ReadBytes using std:string; string s(%1); char sep=/; #ifdef _WIN32 sep=/; #endif size_t sz=s.rfind(sep,s.length(); if(sz!=string:npos) CFile Out; CString strFilename(s.substr(i+1,s.length()-i);

30、 if(Out.Open(%2+“/“+strfilename,cfile:modewrite%7ccfile:modecreate) for(int i=1;i0) int nSize=sizeof(cbBuffer); if(nSizenFilesize) nSize=nFilesize; try In.Read(cbBuffer,nSize); catch(CFileException *e) char *lpMsgBuf; if(FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NUL

31、L,e-m_lOsError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR) LocalFree(lpMsgBuf); e-Delete(); return; try Out.Write(cbBuffer,nSize); catch(CFileException *e) char *lpMsgBuf; if(FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL,e-m_lOsError, MAKELANGID(LANG_NEUTRA

32、L, SUBLANG_DEFAULT), (LPSTR) LocalFree(lpMsgBuf); e-Delete(); return; nFilesize=nSize; else AfxMessageBox(“不能打开“+Filename); else AfxMessageBox(“不能创建输出文件“); 作者:无尾兽零号机 4位粉丝 2009-10-25 10:45 回复此发言10回复:MFC 文件操作大全31.文件简单加密 /#include using std:string; string s(%1); char sep=/; #ifdef _WIN32 sep=/; #endif

33、size_t sz=s.rfind(sep,s.length(); if(sz!=string:npos) CFile Out,In; int nFIlesize; char *lpMsgBuf; CString strFilename(s.substr(i+1,s.length()-i); if(!in.Open(%1,CFile:modeRead) /不能打开输入文件 return; if(!Out.Open(%2+“/enc_%22+strfilename,cfile:modewrite/ | CFile:modeCreate) /不能打开输出文件 return; nFilesize=I

34、n.GetLength(); lpBuffer=new charnFilesize; if(lpBuffer=NULL) /不能分配复制缓存 return; CFileStatus rStatus; In.GetStatus(%1,rStatus); try In.Read(cbBuffer,nFilesize); catch(CFileException *e) char *lpMsgBuf; if(FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL,e-m_lOsError, MA

35、KELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR) LocalFree(lpMsgBuf); e-Delete(); return; for(int i=0;im_lOsError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR) LocalFree(lpMsgBuf); e-Delete(); return; Out.Close(); /In.Close(); CFile:SetStatus(%2+“/enc_%22+strfilename,rstatus); delete lpBuffer;

36、 作者:无尾兽零号机 4位粉丝 2009-10-25 10:45 回复此发言11 回复:MFC 文件操作大全32.文件简单解密 /#include using std:string; string s(%1); char sep=/; #ifdef _WIN32 sep=/; #endif size_t sz=s.rfind(sep,s.length(); if(sz!=string:npos) CFile Out,In; int nFIlesize; char *lpMsgBuf; CString strFilename(s.substr(i+1,s.length()-i); if(!in.

37、Open(%2+“/enc_%22+strfilename,cfile:moderead) /不能打开输入文件 return; if(!Out.Open(%1,CFile:modeWrite | CFile:modeCreate) /不能打开输出文件 return; nFilesize=In.GetLength(); lpBuffer=new charnFilesize; if(lpBuffer=NULL) /不能分配复制缓存 return; CFileStatus rStatus; In.GetStatus(%2+“/enc_%22+strfilename,rstatus); try In.

38、Read(cbBuffer,nFilesize); catch(CFileException *e) char *lpMsgBuf; if(FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,NULL,e-m_lOsError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR) LocalFree(lpMsgBuf); e-Delete(); return; for(int i=0;im_lOsError, MAKELANGID(LANG_NEU

39、TRAL, SUBLANG_DEFAULT), (LPSTR) LocalFree(lpMsgBuf); e-Delete(); return; Out.Close(); /In.Close(); CFile:SetStatus(%1,rStatus); delete lpBuffer; 作者:无尾兽零号机 4位粉丝 2009-10-25 10:45 回复此发言12回复:MFC 文件操作大全33.读取 ini文件属性 CStdioFile inifile(%1,CFile:modeRead); CString path = inifile.GetFilePath(); inifile.Clos

40、e(); char key1024; DWORD bytes = GetPrivateProfileString(%2,%3,%4,key,1024,path); if(bytes 0) int nSize=sizeof(cbBuffer); if(nSizenFilesize) nSize=nFilesize; try In.Read(cbBuffer,nSize); catch(CFileException *e) char *lpMsgBuf; if(FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_S

41、YSTEM, NULL,e-m_lOsError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR) LocalFree(lpMsgBuf); e-Delete(); return; try Out.Write(cbBuffer,nSize); catch(CFileException *e) char *lpMsgBuf; if(FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,NULL,e-m_lOsError, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR) LocalFree(lpMsgBuf); e-Delete(); return; nFilesize=nSize; else AfxMessageBox(“不能打开“+Filename); else AfxMessageBox(“不能创建输出文件“);

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报