mfc文件操作

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 = finder.FindNextFile();
if (finder.IsDirectory())
{
RemoveDirectory(finder.GetFilePath());
}


6.清空文件夹
RemoveDirectory(%%1);
CreateDirectory(%%1,NULL);

7.读取文件
char sRead[1024];
CFile mFile(_T(%%1),CFile::modeRead);
while (sRead!=null)
{
mFile.Read(sRead,1024);
CString %%2(sRead);
%%3
  
 
 

 

2

回复: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(szTempfile,CFile:: modeCreate|CFile:: modeWrite);
char m_char='a';
m_tempFile.Write(&m_char,2);
m_tempFile.Close();
//循环写入多个值
strTempA;
int i;
int nCount=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.读取文件属性
dwAttrs   =   GetFileAttributes(%%1); 
if   (dwAttrs   &   FILE_ATTRIBUTE_READONLY)     {   
   %%2
}     
if   (NORMAL & FILE_ATTRIBUTE_READONLY){   
   %%3
}   

11.写入属性
SetFileAttributes(szNewPath,dwAttrs | FILE_ATTRIBUTE_READONLY);   

12.枚举一个目录下所有文件夹
CFileFind finder; 
BOOL bWorking = finder.FindFile(%%1+"//*.*"); 
while (bWorking) { 
bWorking = finder.FindNextFile();
if(finder.IsDirectory()){
CString %%1=finder.GetFilePath();
%%2
}


13.复制文件夹
WIN32_FIND_DATA FileData; 
HANDLE hSearch; 
DWORD dwAttrs; 
char szDirPath[] = %%2; 
char szNewPath[MAX_PATH]; 
char szHome[MAX_PATH]; 
BOOL fFinished = FALSE; 
if (!CreateDirectory(szDirPath, NULL)) {
//不能创建新的目录 
   return;
}
hSearch = FindFirstFile(%%1+"//*.*", &FileData); 
if (hSearch == INVALID_HANDLE_VALUE) { 
   return; 

while (!fFinished) { 
  
 
 

 

3

回复:MFC文件操作大全

 
   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_DATA FileData; 
HANDLE hSearch; 
DWORD dwAttrs; 
char szDirPath[] = %%2; 
char szNewPath[MAX_PATH]; 
char szHome[MAX_PATH]; 
BOOL fFinished = FALSE; 
if (!CreateDirectory(szDirPath,NULL)) 
{
//不能创建新的目录 
return;
}
BOOL bWorking = finder.FindFile(%%1+"//*.*"); 
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_DATA FileData; 
HANDLE hSearch; 
DWORD dwAttrs; 
char szDirPath[] = %%2; 
char szNewPath[MAX_PATH]; 
char szHome[MAX_PATH]; 
BOOL fFinished = FALSE; 
if (!CreateDirectory(szDirPath, NULL)) 
{
//不能创建新的目录 
return;
}
hSearch = FindFirstFile(%%1+"//*.*", &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);
  
 
 

 

4

回复:MFC文件操作大全

 
16.移动一个文件夹下所有的文件夹到另一个目录下
WIN32_FIND_DATA FileData; 
HANDLE hSearch; 
DWORD dwAttrs; 
char szDirPath[] = %%2; 
char szNewPath[MAX_PATH]; 
char szHome[MAX_PATH]; 
BOOL fFinished = FALSE; 
if (!CreateDirectory(szDirPath,NULL)) 
{
//不能创建新的目录 
return;
}
BOOL bWorking = finder.FindFile(%%1+"//*.*"); 
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_DATA FileData; 
HANDLE hSearch; 
DWORD dwAttrs; 
char szDirPath[] = %%2; 
char szNewPath[MAX_PATH]; 
char szHome[MAX_PATH]; 
BOOL fFinished = FALSE; 
if (!CreateDirectory(szDirPath, NULL)) 
{
//不能创建新的目录 
return;
}
hSearch = FindFirstFile(%%1+"//*.*", &FileData); 
if (hSearch == INVALID_HANDLE_VALUE) 

return; 

while (!fFinished) 

lstrcpy(szNewPath, szDirPath); 
lstrcat(szNewPath, FileData.cFileName); 
HANDLE hFile=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, &FileData))

if (GetLastError() == ERROR_NO_MORE_FILES) 

//遍历文件夹完成 
fFinished = TRUE; 

else 

//找不到下一个文件
return;



FindClose(hSearch); 

18.复制文件
CopyFile(%%1,%%2,true);
  
 
 

 

5

回复:MFC文件操作大全

 
19.复制一个文件夹下所有的文件到另一个目录
//#include <string>
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.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/;
CopyFile(sourcefile.GetBuffer(0),targetfile.GetBuffer(0),true);
}
}


20.提取扩展名
CString path(%%1);
CString %%2=path.Mid(path.ReverseFind('.'));

21.提取文件名
CString path(%%1);
CString %%2=path.Mid(path.ReverseFind('//')+1);

22.提取文件路径
char appName[MAX_PATH];
GetModualFileName(NULL,appName,MAX_PATH);
  
 
 

 

6

回复:MFC文件操作大全

 
23.替换扩展名
//#include <string>
using std::string;
string s(%%1);
string newExt(%%2);
string::size_type i=s.rfind('.',s.length());
if(i!=string::npos)
s.replace(i+1,newExt.length(),newExt);
CString %%3(s); 

24.追加路径
/*
#include <string>
#include <cstdlib>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/fstream.hpp>
*/
using namespace std;
using namespace boost::filesystem;
try {
path p1=complete(path(%%2,native),
path(%%1,native));
path p2=system_complete(path(%%2,native));
CString %%3(p3);
}
catch(exception& e){
//e.what();


25.移动文件
MoveFile(%%1,%%2);
  
 
 

 

7

回复:MFC文件操作大全

 
26.移动一个文件夹下所有文件到另一个目录
//#include <string>
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.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/;
MoveFile(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();
}
  
 
 

 

8

回复: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 pathName(" ",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 pBuf[4096];
if(m_File.Open(m_FileName,CFile::modeRead | CFile::shareDenyWrite))
{
m_FileName=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);
wrFile.Open(strName,CFile::modeWrite | CFile::modeCreate);
wrFile.Write(pBuf,ReadBytes);
wrFile.Close();
if(ReadBytes<PartLength)
break;
nCount++;
}
m_File.Close();
}
else
AfxMessageBox("不能打开文件");
  
 
 

 

9

回复:MFC文件操作大全

 
30.文件合并
//#include <string>
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));
if(Out.Open(%%2+"//"+strfilename,cfile::modewrite%7ccfile::modecreate)){
for(int i=1;i<=2;i++)
{
String Filename=%%%2+"//"+strfilename+atoi(i);
CFile In;
if(In.Open(Filename,CFile::modeRead)){
char cbBuffer[4096];
int nFilesize=In.GetLength();
while(nFilesize>0){
int nSize=sizeof(cbBuffer);
if(nSize>nFilesize)
nSize=nFilesize;
try{
In.Read(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)&lpMsgBuf,0,NULL)>0){
AfxMessageBox(lpMsgBuf);
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)&lpMsgBuf,0,NULL)>0){
AfxMessageBox(lpMsgBuf);
LocalFree(lpMsgBuf);
}
e->Delete();
return;
}
nFilesize=nSize;
}
}
else
AfxMessageBox("不能打开"+Filename);
}
}
else
AfxMessageBox("不能创建输出文件");
}
  
 
 

 

10

回复:MFC文件操作大全

 
31.文件简单加密
//#include <string>
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.Open(%%1,CFile::modeRead)){
//不能打开输入文件
return;
}
if(!Out.Open(%%2+"//enc_%22+strfilename,cfile::modewrite/ | CFile::modeCreate)){
//不能打开输出文件
return;
}
nFilesize=In.GetLength();
lpBuffer=new char[nFilesize];
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,
MAKELANGID(LANG_NEUTRAL,
SUBLANG_DEFAULT),
(LPSTR)&lpMsgBuf,0,NULL)>0){
AfxMessageBox(lpMsgBuf);
LocalFree(lpMsgBuf);
}
e->Delete();
return;
}
for(int i=0;i<nFilesize;i++)
{
int ibt=lpBuffer[i];
ibt+=100;
ibt%=256;
bpBuffer[i]=(char)ibt;
}
try{
Out.Write(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)&lpMsgBuf,0,NULL)>0){
AfxMessageBox(lpMsgBuf);
LocalFree(lpMsgBuf);
}
e->Delete();
return;
}
Out.Close();
//In.Close();
CFile::SetStatus(%%2+"//enc_%22+strfilename,rstatus);
 delete[] lpBuffer;
}
  
 
 

 

11

回复:MFC文件操作大全

 
32.文件简单解密
//#include <string>
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.Open(%%2+"//enc_%22+strfilename,cfile::moderead)){
//不能打开输入文件
return;
}
if(!Out.Open(%%1,CFile::modeWrite | CFile::modeCreate)){
//不能打开输出文件
return;
}
nFilesize=In.GetLength();
lpBuffer=new char[nFilesize];
if(lpBuffer==NULL){
//不能分配复制缓存
return;
}
CFileStatus rStatus;
In.GetStatus(%%2+"//enc_%22+strfilename,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,
MAKELANGID(LANG_NEUTRAL,
SUBLANG_DEFAULT),
(LPSTR)&lpMsgBuf,0,NULL)>0){
AfxMessageBox(lpMsgBuf);
LocalFree(lpMsgBuf);
}
e->Delete();
return;
}
for(int i=0;i<nFilesize;i++)
{
int ibt=lpBuffer[i];
ibt-=100;ibt+=256;
ibt%=256;
bpBuffer[i]=(char)ibt;
}
try{
Out.Write(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)&lpMsgBuf,0,NULL)>0){
AfxMessageBox(lpMsgBuf);
LocalFree(lpMsgBuf);
}
e->Delete();
return;
}
Out.Close();
//In.Close();
CFile::SetStatus(%%1,rStatus);
delete[] lpBuffer;
}
  
 
 

 

12

回复:MFC文件操作大全

 
33.读取ini文件属性
CStdioFile inifile(%%1,CFile::modeRead);
CString path = inifile.GetFilePath();
inifile.Close();
char key[1024];
DWORD bytes = GetPrivateProfileString(%%2,%%3,%%4,key,1024,path);
if(bytes < 1024)
key[bytes] = '/0';
CString %%5(key); 

34.合并一个文件下所有的文件
CString Directory=%%1+"*.*";
CFileFind FFile;
CFile Out;
if(Out.Open(%%2,CFile::modeWrite|CFile::modeCreate)){
BOOL bFound=FFile.FindFile(Directory);
while(bFound)
{
bFound=FFile.FileNextFile();
if(!FFile.IsDirectory() && !FFile.IsDots())
{
CString Filename=FFile.GetFileName();
CFile In;
if(In.Open(Filename,CFile::modeRead)){
char cbBuffer[4096];
int nFIlesize=In.GetLength();
while(nFIlesize>0){
{
int nSize=sizeof(cbBuffer);
if(nSize>nFilesize)
nSize=nFilesize;
try {
In.Read(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)&lpMsgBuf,0,NULL)>0){
AfxMessageBox(lpMsgBuf);
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)&lpMsgBuf,0,NULL)>0){
AfxMessageBox(lpMsgBuf);
LocalFree(lpMsgBuf);
}
e->Delete();
return;
}
nFilesize=nSize;
}
}
else
AfxMessageBox("不能打开"+Filename);
}
}
}
else
AfxMessageBox("不能创建输出文件");
 
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: VC 6.0是微软公司开发的一款集成开发环境,MFC(Microsoft Foundation Classes)是其一部分,用于开发Windows平台上的C++应用程序。在VC 6.0中,MFC提供了丰富的文件操作功能,以便开发者可以方便地进行文件的读写和处理。 MFC中的文件操作主要通过CFile类来实现。要进行文件操作,首先需要创建一个CFile对象,并指定文件名和打开模式。打开模式可以是读取(CFile::modeRead)、写入(CFile::modeWrite)、读写(CFile::modeReadWrite)等。然后,可以通过CFile对象调用相应的方法来实现文件的读写操作。 常用的文件操作方法包括Read、ReadString、ReadHuge等。其中,Read方法用于读取指定数量的字节到缓冲区中,ReadString方法用于读取一行字符串,ReadHuge方法用于读取大型文件。在读取文件前,需要使用CFileOpen方法打开文件,读取完成后,需要调用Close方法关闭文件文件操作方法包括Write、WriteString、WriteHuge等。其中,Write方法用于将指定数量的字节从缓冲区写入文件中,WriteString方法用于将字符串写入文件,WriteHuge方法用于写入大型文件。在写入文件前,也需要使用CFileOpen方法打开文件,写入完成后,同样需要调用Close方法关闭文件。 此外,MFC还提供了其他一些文件操作的辅助功能,如文件的创建与删除、文件的属性设置、文件指针的定位等。 总之,VC 6.0中的MFC文件操作功能强大且灵活,可以满足各种文件操作的需求。开发者可以根据具体的应用场景,选择适合的文件操作方法,并结合其他MFC的功能,实现更加复杂的文件处理功能。 ### 回答2: VC 6.0是微软的一款集成开发环境,MFC(Microsoft Foundation Classes)是一套用于开发Windows应用程序的C++类库。在VC 6.0中,我们可以使用MFC进行文件操作MFC提供了一系列的类来进行文件的创建、打开、读取、写入和关闭等操作。首先,我们可以使用CFile类来创建或打开一个文件,并进行读写操作。我们可以使用Open函数来打开一个已经存在的文件或者使用Create函数来创建一个新的文件。然后,可以使用Read和Write函数进行数据的读取和写入。最后,使用Close函数关闭文件。通过这些函数,我们可以灵活地操作文件。 另外,MFC还提供了CStdioFile类,它是对CFile类的一个高级封装。使用CStdioFile类,我们可以更方便地进行文本文件的读写操作。例如,可以使用ReadString函数逐行读取文本文件的内容,使用WriteString函数将字符串写入文本文件。 此外,MFC还提供了CFileDialog类,用于显示文件选择对话框,方便用户选择文件。通过这个类,我们可以让用户选择要打开或保存的文件,并获取用户选择的文件路径。 总结起来,VC 6.0的MFC提供了丰富的文件操作类和函数,可以方便地进行文件的读写,包括二进制文件和文本文件。开发者可以根据自己的需要使用相应的类和函数来操作文件,以完成各种文件处理任务。 ### 回答3: VC 6.0的MFC(Microsoft Foundation Class)库提供了一些用于文件操作的类和函数,方便开发人员在Windows平台上进行文件的读写和管理。下面是一些常用的MFC文件操作相关的功能和类。 1. CFile类:CFile类是MFC提供的用于文件操作的基本类,它封装了文件的打开、读写、关闭等功能。通过CFile类的派生类CStdioFile可以实现对文本文件的读写操作。通过CFile类的派生类CMemFile可以在内存中创建文件。 2. CString类:CString类是MFC中用于处理字符串的类,可以将字符串写入文件或从文件中读取字符串。通过CFile类的WriteString()和ReadString()函数可以方便地进行字符串的写入和读取。 3. CArchive类:CArchive类使开发人员能够方便地对对象进行序列化和反序列化操作。通过CArchive类的派生类CFile和CMemFile可以将对象保存到文件中或从文件中读取对象。 4. 文件打开对话框:MFC提供了CFileDialog类,可以通过它打开一个文件选择对话框,选择需要进行操作文件路径和文件名。 5. 文件属性操作:通过CFile类的相关函数可以获取文件的属性信息,如文件的大小、创建时间、修改时间等。 6. 文件夹和目录操作MFC通过CFileFind类提供了对文件夹和目录的操作,可以遍历文件夹中的文件或子文件夹。 除了上述基本的文件操作类和函数外,MFC还提供了一系列的扩展类和函数,用于处理文件的权限、文件夹的创建和删除等操作。开发人员可以根据具体的需求选择合适的类和函数进行文件操作。但需要注意的是,VC 6.0已经较为古老,一些新的文件操作方式和技术,如C++的文件流和Boost库等,可能在VC 6.0中并不适用。因此建议尽量使用更新的版本进行文件操作
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值