mfc文件操作.docx

上传人:b****1 文档编号:10466036 上传时间:2023-05-26 格式:DOCX 页数:99 大小:41.80KB
下载 相关 举报
mfc文件操作.docx_第1页
第1页 / 共99页
mfc文件操作.docx_第2页
第2页 / 共99页
mfc文件操作.docx_第3页
第3页 / 共99页
mfc文件操作.docx_第4页
第4页 / 共99页
mfc文件操作.docx_第5页
第5页 / 共99页
mfc文件操作.docx_第6页
第6页 / 共99页
mfc文件操作.docx_第7页
第7页 / 共99页
mfc文件操作.docx_第8页
第8页 / 共99页
mfc文件操作.docx_第9页
第9页 / 共99页
mfc文件操作.docx_第10页
第10页 / 共99页
mfc文件操作.docx_第11页
第11页 / 共99页
mfc文件操作.docx_第12页
第12页 / 共99页
mfc文件操作.docx_第13页
第13页 / 共99页
mfc文件操作.docx_第14页
第14页 / 共99页
mfc文件操作.docx_第15页
第15页 / 共99页
mfc文件操作.docx_第16页
第16页 / 共99页
mfc文件操作.docx_第17页
第17页 / 共99页
mfc文件操作.docx_第18页
第18页 / 共99页
mfc文件操作.docx_第19页
第19页 / 共99页
mfc文件操作.docx_第20页
第20页 / 共99页
亲,该文档总共99页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

mfc文件操作.docx

《mfc文件操作.docx》由会员分享,可在线阅读,更多相关《mfc文件操作.docx(99页珍藏版)》请在冰点文库上搜索。

mfc文件操作.docx

mfc文件操作

1.创建文件夹

CreateDirectory(%%1,NULL);稳哥有错误自己调试啊。

2.创建文件

CFilefile;

file.Open(%%1,CFile:

:

modeCreate|CFile:

:

modeWrite);

3.删除文件

DeleteFile(%%1);

4.删除文件夹

RemoveDirectory(%%1);

5.删除一个目录下所有的文件夹

CFileFindfinder;

CStringpath;

path.Format("%s\\*.*",%%1);

BOOLbWorking=finder.FindFile(path);

while(bWorking)

{

bWorking=finder.FindNextFile();

if(finder.IsDirectory())

{

RemoveDirectory(finder.GetFilePath());

}

}

6.清空文件夹

RemoveDirectory(%%1);

CreateDirectory(%%1,NULL);

7.读取文件

charsRead[5120];

CFilemFile(_T(%%1),CFile:

:

modeRead);

while(sRead!

=NULL)

{

mFile.Read(sRead,5120);

CString%%2(sRead);

%%3

}

mFile.Close();

8.写入文件

CFilemFile(_T(%%1),CFile:

:

modeWrite|CFile:

:

modeCreate);

mFile.Write(%%2,sizeof(%%2));

mFile.Flush();

mFile.Close();

9.写入随机文件

charszTempPath[_MAX_PATH],szTempfile[_MAX_PATH];

GetTempPath(_MAX_PATH,szTempPath);

GetTempFileName(szTempPath,_T("my_"),0,szTempfile);

CFilem_tempFile(szTempfile,CFile:

:

modeCreate|CFile:

:

modeWrite);

charm_char='a';

m_tempFile.Write(&m_char,2);

m_tempFile.Close();

//循环写入多个值

strTempA;

inti;

intnCount=6;

//共有6个文件名需要保存

for(i=0;i{strTemp.Format("%d",i);

strTempA=文件名;

//文件名可以从数组,列表框等处取得.

:

:

WritePrivateProfileString("UseFileName","FileName"+strTemp,strTempA,

c:

\\usefile\\usefile.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;i{strTemp.Format("%d",i);

strTemp="FileName"+strTemp;

:

:

GetPrivateProfileString("CurrentIni",strTemp,"default.fil",strTempA.GetBuffer(MAX_PATH),MAX_PATH,"c:

\\usefile\\usefile.ini");

//使用strTempA中的内容.

}

10.读取文件属性

DWORDdwAttrs=GetFileAttributes(%%1);

if(dwAttrs&FILE_ATTRIBUTE_READONLY){

%%2

}

if(dwAttrs&FILE_ATTRIBUTE_NORMAL){

%%3

}

11.写入属性

SetFileAttributes(%%1,dwAttrs|FILE_ATTRIBUTE_READONLY);

12.枚举一个目录下所有文件夹

CFileFindfinder;

CStringpath;

path.Format("%s\\*.*",%%1);

BOOLbWorking=finder.FindFile(path);

while(bWorking){

bWorking=finder.FindNextFile();

if(finder.IsDirectory()){

CString%%1=finder.GetFilePath();

%%2

}

}

13.复制文件夹

WIN32_FIND_DATAFileData;

HANDLEhSearch;

DWORDdwAttrs;

charszDirPath[]=%%2;

charszNewPath[MAX_PATH];

charszHome[MAX_PATH];

BOOLfFinished=FALSE;

if(!

CreateDirectory(szDirPath,NULL)){

//不能创建新的目录

return;

}

CStringpath;

path.Format("%s\\*.*",%%1);

hSearch=FindFirstFile(path,&FileData);

if(hSearch==INVALID_HANDLE_VALUE){

return;

}

while(!

fFinished){

lstrcpy(szNewPath,szDirPath);

lstrcat(szNewPath,FileData.cFileName);

if(CopyFile(FileData.cFileName,szNewPath,FALSE)){

dwAttrs=GetFileAttributes(FileData.cFileName);

if(!

(dwAttrs&FILE_ATTRIBUTE_READONLY)){

SetFileAttributes(szNewPath,

dwAttrs|FILE_ATTRIBUTE_READONLY);

}

}

else{

//不能复制文件

return;

}

if(!

FindNextFile(hSearch,&FileData)){

if(GetLastError()==ERROR_NO_MORE_FILES){

//遍历文件夹完成

fFinished=TRUE;

}

else{

//找不到下一个文件

return;

}

}

}

FindClose(hSearch);

14.复制一个目录下所有的文件夹到另一个文件夹下

WIN32_FIND_DATAFileData;

HANDLEhSearch;

DWORDdwAttrs;

charszDirPath[]=%%2;

charszNewPath[MAX_PATH];

charszHome[MAX_PATH];

BOOLfFinished=FALSE;

if(!

CreateDirectory(szDirPath,NULL))

{

//不能创建新的目录

return;

}

CStringpath;

path.Format("%s\\*.*",%%1);

BOOLbWorking=finder.FindFile(path);

while(bWorking)

{

bWorking=finder.FindNextFile();

if(finder.IsDirectory()){

hSearch=FindFirstFile(finder.GetFilePath()+"\\*.*",&FileData);

if(hSearch==INVALID_HANDLE_VALUE)

{

return;

}

while(!

fFinished)

{

lstrcpy(szNewPath,szDirPath);

lstrcat(szNewPath,FileData.cFileName);

if(CopyFile(FileData.cFileName,szNewPath,FALSE))

{

dwAttrs=GetFileAttributes(FileData.cFileName);

if(!

(dwAttrs&FILE_ATTRIBUTE_READONLY))

{

SetFileAttributes(szNewPath,

dwAttrs|FILE_ATTRIBUTE_READONLY);

}

}

else

{

//不能复制文件

return;

}

if(!

FindNextFile(hSearch,&FileData))

{

if(GetLastError()==ERROR_NO_MORE_FILES)

{

//遍历文件夹完成

fFinished=TRUE;

}

else

{

//找不到下一个文件

return;

}

}

}

FindClose(hSearch);

}

}

15.移动文件夹

WIN32_FIND_DATAFileData;

HANDLEhSearch;

DWORDdwAttrs;

charszDirPath[]=%%2;

charszNewPath[MAX_PATH];

charszHome[MAX_PATH];

BOOLfFinished=FALSE;

if(!

CreateDirectory(szDirPath,NULL))

{

//不能创建新的目录

return;

}

CStringpath;

path.Format("%s\\*.*",%%1);

hSearch=FindFirstFile(path,&FileData);

if(hSearch==INVALID_HANDLE_VALUE)

{

return;

}

while(!

fFinished)

{

lstrcpy(szNewPath,szDirPath);

lstrcat(szNewPath,FileData.cFileName);

if(CopyFile(FileData.cFileName,szNewPath,FALSE))

{

dwAttrs=GetFileAttributes(FileData.cFileName);

if(!

(dwAttrs&FILE_ATTRIBUTE_READONLY))

{

SetFileAttributes(szNewPath,

dwAttrs|FILE_ATTRIBUTE_READONLY);

}

}

else

{

//不能复制文件

return;

}

if(!

FindNextFile(hSearch,&FileData))

{

if(GetLastError()==ERROR_NO_MORE_FILES)

{

//遍历文件夹完成

fFinished=TRUE;

}

else

{

//找不到下一个文件

return;

}

}

}

FindClose(hSearch);

RemoveDirectory(%%1);

16.移动一个文件夹下所有的文件夹到另一个目录下

WIN32_FIND_DATAFileData;

HANDLEhSearch;

DWORDdwAttrs;

charszDirPath[]=%%2;

charszNewPath[MAX_PATH];

charszHome[MAX_PATH];

BOOLfFinished=FALSE;

if(!

CreateDirectory(szDirPath,NULL))

{

//不能创建新的目录

return;

}

CStringpath;

path.Format("%s\\*.*",%%1);

BOOLbWorking=finder.FindFile(path);

while(bWorking)

{

bWorking=finder.FindNextFile();

if(finder.IsDirectory()){

hSearch=FindFirstFile(finder.GetFilePath()+"\\*.*",&FileData);

if(hSearch==INVALID_HANDLE_VALUE)

{

return;

}

while(!

fFinished)

{

lstrcpy(szNewPath,szDirPath);

lstrcat(szNewPath,FileData.cFileName);

if(CopyFile(FileData.cFileName,szNewPath,FALSE))

{

dwAttrs=GetFileAttributes(FileData.cFileName);

if(!

(dwAttrs&FILE_ATTRIBUTE_READONLY))

{

SetFileAttributes(szNewPath,

dwAttrs|FILE_ATTRIBUTE_READONLY);

}

}

else

{

//不能复制文件

return;

}

if(!

FindNextFile(hSearch,&FileData))

{

if(GetLastError()==ERROR_NO_MORE_FILES)

{

//遍历文件夹完成

fFinished=TRUE;

}

else

{

//找不到下一个文件

return;

}

}

}

FindClose(hSearch);

RemoveDirectory(finder.GetFilePath().GetBuffer(0));

}

}

17.以一个文件夹的框架在另一个目录创建文件夹和空文件

WIN32_FIND_DATAFileData;

HANDLEhSearch;

DWORDdwAttrs;

charszDirPath[]=%%2;

charszNewPath[MAX_PATH];

charszHome[MAX_PATH];

BOOLfFinished=FALSE;

if(!

CreateDirectory(szDirPath,NULL))

{

//不能创建新的目录

return;

}

CStringpath;

path.Format("%s\\*.*",%%1);

hSearch=FindFirstFile(path,&FileData);

if(hSearch==INVALID_HANDLE_VALUE)

{

return;

}

while(!

fFinished)

{

lstrcpy(szNewPath,szDirPath);

lstrcat(szNewPath,FileData.cFileName);

HANDLEhFile=CreateFileHandlehFile=CreateFile(szNewPath,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL|FILE_FLAG_SEQUENTIAL_SCAN,NULL);

if(!

hFile)

{

//不能创建文件

return;

}

if(!

FindNextFile(hSearch,&FileData))

{

if(GetLastError()==ERROR_NO_MORE_FILES)

{

//遍历文件夹完成

fFinished=TRUE;

}

else

{

//找不到下一个文件

return;

}

}

}

FindClose(hSearch);

18.复制文件

CopyFile(%%1,%%2,true);

19.复制一个文件夹下所有的文件到另一个目录

//#include

usingstd:

:

string;

charsep='/';

#ifdef_WIN32

sep='\\';

#endif

CFileFindfinder;

CStringpath;

path.Format("%s\\*.*",%%1);

BOOLbWorking=finder.FindFile(path);

while(bWorking)

{

bWorking=finder.FindNextFile();

if(!

finder.IsDirectory()||finder.IsDots()){

strings(finder.GetFileName());

CStringsourcefile(%%1);

if(s.rfind(sep,s.length())!

=string:

:

npos)

{

sourcefile=sourcefile+"//"+s.substr(i+1,s.length()-i);

CStringtargetfile(s.substr(i+1,s.length()-i));

targetfile=%%2+"//"+targetfile/;

CopyFile(sourcefile.GetBuffer(0),targetfile.GetBuffer(0),true);

}

}

}

20.提取扩展名

CStringpath(%%1);

CString%%2=path.Mid(path.ReverseFind('.'));

21.提取文件名

CStringpath(%%1);

CString%%2=path.Mid(path.ReverseFind('\\')+1);

22.提取文件路径

charappName[MAX_PATH];

GetModualFileName(NULL,appName,MAX_PATH);

23.替换扩展名

//#include

usingstd:

:

string;

strings(%%1);

stringnewExt(%%2);

string:

:

size_typei=s.rfind('.',s.length());

if(i!

=string:

:

npos)

s.replace(i+1,newExt.length(),newExt);

CString%%3(s);

24.追加路径

/*

#include

#include

#include

#include

*/

usingnamespacestd;

usingnamespaceboost:

:

filesystem;

try{

pathp1=complete(path(%%2,native),

path(%%1,native));

pathp2=system_complete(path(%%2,native));

CString%%3(p3);

}

catch(exception&e){

//e.what();

}

25.移动文件

MoveFile(%%1,%%2);

26.移动一个文件夹下所有文件到另一个目录

//#include

usingstd:

:

string;

charsep='/';

#ifdef_WIN32

sep='\\';

#endif

CFileFindfinder;

CStringpath;

path.Format("%s\\*.*",%%1);

BOOLbWorking=finder.FindFile(path);

while(bWorking)

{

bWorking=finder.FindNextFile();

if(!

finder.IsDirectory()||finder.IsDots()){

strings(finder.GetFileName());

CStringsourcefile(%%1);

if(s.rfind(sep,s.length())!

=string:

:

npos)

{

sourcefile=sourcefile+"//"+s.substr(i+1,s.length()-i);

CStringtargetfile(s.substr(i+1,s.length()-i));

targetfile=%%2+"//"+targetfile/;

MoveFile(sourcefile.GetBuffer(0),targetfile.GetBuffer(0),true);

}

}

}

27.指定目录下搜索文件

CStringstrFileTitle;

CFileFindfinder;

BOOLbWorking=finder.FindFile(%%1);//"C:

\\windows\\sysbkup\\*.cab"

while(bWorking)

{

bWorking=finder.FindNextFile();

strFileTitle=finder.GetFileTitle();

}

28.打开对话框

CFileDialogmFileDlg(TRUE,NULL,NULL,OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT,"AllFiles(*.*)|*.*||",AfxGetMainWnd());

CStringstr("",10000);

mFileDlg.m_ofn.lpstrFile=str.GetBuffer(10000);

str.ReleaseBuffer();

POSITIONmPo

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

当前位置:首页 > PPT模板 > 动态背景

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2