⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vc中常用文件操作(转载) - yfwei的专栏 - csdnblog.htm

📁 demod tuner sdfasdfasdfdsf
💻 HTM
📖 第 1 页 / 共 3 页
字号:
  <DD><A 
  href="http://www.netvibes.com/subscribe.php?url=http://blog.csdn.net/yfwei/Rss.aspx" 
  target=_blank><IMG alt=订阅到netvibes 
  src="vc中常用文件操作(转载) - yfwei的专栏 - CSDNBlog.files/rss_netvibes.gif" 
  border=0></A></SPAN> </DD></DL></DIV></DIV></DIV>
<DIV id=csdnblog_content>
<DIV class=gutter>
<DIV class=default_contents>
<DIV class=user_article>
<SCRIPT src="vc中常用文件操作(转载) - yfwei的专栏 - CSDNBlog.files/LoadFeedbackCount.js" 
type=text/javascript></SCRIPT>

<H1><IMG height=14 alt=转载 
src="vc中常用文件操作(转载) - yfwei的专栏 - CSDNBlog.files/turnship.gif" width=15 
border=0>&nbsp;<A 
href="http://blog.csdn.net/yfwei/archive/2007/09/05/1772605.aspx">vc中常用文件操作(转载)</A><CITE 
class=fav_csdnstylebykimi><A class=fav_csdnstylebykimi title=收藏到我的网摘中,并分享给我的朋友 
href="javascript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(saveit=window.open('http://wz.csdn.net/storeit.aspx?t='+escape(d.title)+'&amp;u='+escape(d.location.href)+'&amp;c='+escape(t),'saveit','scrollbars=no,width=590,height=300,left=75,top=20,status=no,resizable=yes'));saveit.focus();">收藏</A></CITE></H1>
<DIV class=blogstory><SPAN id=Post.ascx_ViewPost_PreviousAndNextEntriesUp>
<H3><A 
href="http://blog.csdn.net/yfwei/archive/2007/09/05/1772902.aspx">新一篇:&nbsp;STL中map用法详解(转载)</A>&nbsp;|&nbsp;<A 
href="http://blog.csdn.net/yfwei/archive/2007/09/04/1771658.aspx">旧一篇:&nbsp;CString 
常用函数(转载)</A></H3></SPAN>
<SCRIPT>function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}</SCRIPT>

<P>(本文全部摘自internet)&nbsp;</P>
<P><A id=articleTitle><FONT size=3><STRONG>vc中常用文件操作(一)</STRONG></FONT></A> </P>
<P>各种关于文件的操作在程序设计中是十分常见,如果能对其各种操作都了如指掌,就可以根据实际情况找到最佳的解决方案,从而在较短的时间内编写出高效的代码,因而熟练的掌握文件操作是十分重要的。本文将对Visual 
C++中有关文件操作进行全面的介绍,并对在文件操作中经常遇到的一些疑难问题进行详细的分析。</P>
<P><BR><FONT style="FONT-SIZE: 14px" 
size=3>  1.文件的查找<BR>  当对一个文件操作时,如果不知道该文件是否存在,就要首先进行查找。MFC中有一个专门用来进行文件查找的类CFileFind,使用它可以方便快捷地进行文件的查找。下面这段代码演示了这个类的最基本使用方法。<BR>  CString 
strFileTitle;<BR>  CFileFind finder;<BR>  BOOL bWorking = finder.FindFile&nbsp; 
("C:\\windows\\sysbkup\\*.cab");<BR>  while(bWorking)<BR>  {<BR>  bWorking=finder.FindNextFile();<BR>  strFileTitle=finder.GetFileTitle();<BR>  }</FONT></P>
<P><BR><FONT style="FONT-SIZE: 14px" 
size=3>  2.文件的打开/保存对话框<BR>  让用户选择文件进行打开和存储操作时,就要用到文件打开/保存对话框。MFC的类CFileDialog用于实现这种功能。使用CFileDialog声明一个对象时,第一个BOOL型参数用于指定文件的打开或保存,当为TRUE时将构造一个文件打开对话框,为FALSE时构造一个文件保存对话框。<BR>  在构造CFileDialog对象时,如果在参数中指定了OFN_ALLOWMULTISELECT风格,则在此对话框中可以进行多选操作。此时要重点注意为此CFileDialog对象的m_ofn.lpstrFile分配一块内存,用于存储多选操作所返回的所有文件路径名,如果不进行分配或分配的内存过小就会导致操作失败。下面这段程序演示了文件打开对话框的使用方法。<BR>  CFileDialog 
mFileDlg(TRUE,NULL,NULL,<BR>  OFN_HIDEREADONLY|OFN_OVERWRITEPROMPT|OFN_ALLOWMULTISELECT,<BR>  "All 
Files (*.*)|*.*||",AfxGetMainWnd());<BR>  CString str(" 
",10000);<BR>  mFileDlg.m_ofn.lpstrFile=str.GetBuffer(10000);<BR>  str.ReleaseBuffer();<BR>  POSITION 
mPos=mFileDlg.GetStartPosition();<BR>  CString pathName(" 
",128);<BR>  CFileStatus status;<BR>  while(mPos!=NULL)<BR>  {<BR>  &nbsp; 
pathName=mFileDlg.GetNextPathName(mPos);<BR>  &nbsp; CFile::GetStatus( pathName, 
status );<BR>  }</FONT></P>
<P><BR><FONT style="FONT-SIZE: 14px" 
size=3>  3.文件的读写<BR>  文件的读写非常重要,下面将重点进行介绍。文件读写的最普通的方法是直接使用CFile进行,如文件的读写可以使用下面的方法:<BR>  //对文件进行读操作<BR>  char 
sRead[2];<BR>  CFile 
mFile(_T("user.txt"),CFile::modeRead);<BR>  if(mFile.GetLength()&lt;2)<BR>  return;<BR>  mFile.Read(sRead,2);<BR>  mFile.Close();<BR>  //对文件进行写操作<BR>  CFile 
mFile(_T("user.txt "),&nbsp; 
CFile::modeWrite|CFile::modeCreate);<BR>  mFile.Write(sRead,2);<BR>  mFile.Flush();<BR>  mFile.Close();<BR>  虽然这种方法最为基本,但是它的使用繁琐,而且功能非常简单</FONT></P>
<P><FONT 
style="FONT-SIZE: 14px">4.临时文件的使用<BR><BR>  正规软件经常用到临时文件,你经常可以会看到C:\Windows\Temp目录下有大量的扩展名为tmp的文件,这些就是程序运行是建立的临时文件。临时文件的使用方法基本与常规文件一样,只是文件名应该调用函数GetTempFileName()获得。它的第一个参数是建立此临时文件的路径,第二个参数是建立临时文件名的前缀,第四个参数用于得到建立的临时文件名。得到此临时文件名以后,你就可以用它来建立并操作文件了,如:<BR>  char 
szTempPath[_MAX_PATH],szTempfile[_MAX_PATH];<BR>  GetTempPath(_MAX_PATH, 
szTempPath);<BR>  GetTempFileName(szTempPath,_T 
("my_"),0,szTempfile);<BR>  CFile m_tempFile(szTempfile,CFile:: 
modeCreate|CFile:: modeWrite);<BR>  char 
m_char='a';<BR>  m_tempFile.Write(&amp;m_char,2);<BR><FONT 
style="FONT-SIZE: 14px">  m_tempFile.Close();<BR>  5.文件的复制、删除等<BR>  MFC中没有提供直接进行这些操作的功能,因而要使用SDK。SDK中的文件相关函数常用的有CopyFile()、CreateDirectory()、DeleteFile()、MoveFile()。它们的用法很简单,可参考MSDN。</FONT></FONT></P>
<DIV class=invisible id=reference><A id=articleTitle><FONT 
size=3><STRONG>vc中常用文件操作(二)&nbsp;Ini文件操作</STRONG></FONT></A> </DIV>
<DIV 
class=invisible>在我们写的程序当中,总有一些配置信息需要保存下来,以便完成程序的功能,最简单的办法就是将这些信息写入INI文件中,程序初始化时再读入.具体应用如下: 

<P><FONT style="FONT-SIZE: 14px" size=3>  一.将信息写入.INI文件中.</FONT></P>
<P><FONT style="FONT-SIZE: 14px" size=3>  1.所用的WINAPI函数原型为:</FONT></P>
<P><FONT style="FONT-SIZE: 14px" size=3>BOOL 
WritePrivateProfileString(<BR>LPCTSTR lpAppName,<BR>LPCTSTR 
lpKeyName,<BR>LPCTSTR lpString,<BR>LPCTSTR lpFileName<BR>);</FONT></P>
<P><FONT style="FONT-SIZE: 14px" size=3>  其中各参数的意义:</FONT></P>
<P><FONT style="FONT-SIZE: 14px" size=3>   LPCTSTR lpAppName 
是INI文件中的一个字段名.</FONT></P>
<P><FONT style="FONT-SIZE: 14px" size=3>   LPCTSTR lpKeyName 
是lpAppName下的一个键名,通俗讲就是变量名.</FONT></P>
<P><FONT style="FONT-SIZE: 14px" size=3>   LPCTSTR lpString 
是键值,也就是变量的值,不过必须为LPCTSTR型或CString型的.</FONT></P>
<P><FONT style="FONT-SIZE: 14px" size=3>   LPCTSTR lpFileName 
是完整的INI文件名.</FONT></P>
<P><FONT style="FONT-SIZE: 14px" size=3>  2.具体使用方法:设现有一名学生,需把他的姓名和年龄写入 
c:\stud\student.ini 文件中.</FONT></P>
<P><FONT style="FONT-SIZE: 14px" size=3>CString strName,strTemp;<BR>int 
nAge;<BR>strName="张三";<BR>nAge=12;<BR>::WritePrivateProfileString("StudentInfo","Name",strName,"c:\\stud\\student.ini");</FONT></P>
<P><FONT style="FONT-SIZE: 14px" 
size=3>  此时c:\stud\student.ini文件中的内容如下:</FONT></P>
<P><FONT style="FONT-SIZE: 14px" 
size=3>   [StudentInfo]<BR>   Name=张三</FONT></P>
<P><FONT style="FONT-SIZE: 14px" 
size=3>  3.要将学生的年龄保存下来,只需将整型的值变为字符型即可:</FONT></P>
<P><FONT style="FONT-SIZE: 14px" 
size=3>strTemp.Format("%d",nAge);<BR>::WritePrivateProfileString("StudentInfo","Age",strTemp,"c:\\stud\\student.ini");</FONT></P>
<P><BR><FONT style="FONT-SIZE: 14px" size=3> 二.将信息从INI文件中读入程序中的变量.</FONT></P>
<P><FONT style="FONT-SIZE: 14px" size=3>  1.所用的WINAPI函数原型为:</FONT></P>
<P><FONT style="FONT-SIZE: 14px" size=3>DWORD 
GetPrivateProfileString(<BR>LPCTSTR lpAppName,<BR>LPCTSTR lpKeyName,<BR>LPCTSTR 
lpDefault,<BR>LPTSTR lpReturnedString,<BR>DWORD nSize,<BR>LPCTSTR 
lpFileName<BR>);</FONT></P>
<P><FONT style="FONT-SIZE: 14px" size=3>  其中各参数的意义:</FONT></P>
<P><FONT style="FONT-SIZE: 14px" size=3>   前二个参数与 
WritePrivateProfileString中的意义一样.</FONT></P>
<P><FONT style="FONT-SIZE: 14px" size=3>   lpDefault : 
如果INI文件中没有前两个参数指定的字段名或键名,则将此值赋给变量.</FONT></P>
<P><FONT style="FONT-SIZE: 14px" size=3>   lpReturnedString : 
接收INI文件中的值的CString对象,即目的缓存器.</FONT></P>
<P><FONT style="FONT-SIZE: 14px" size=3>   nSize : 目的缓存器的大小.</FONT></P>
<P><FONT style="FONT-SIZE: 14px" size=3>   lpFileName : 是完整的INI文件名.</FONT></P>
<P><FONT style="FONT-SIZE: 14px" 
size=3>  2.具体使用方法:现要将上一步中写入的学生的信息读入程序中.</FONT></P>
<P><FONT style="FONT-SIZE: 14px" size=3>CString strStudName;<BR>int 
nStudAge;<BR>GetPrivateProfileString("StudentInfo","Name","默认姓名",strStudName.GetBuffer(MAX_PATH),MAX_PATH,"c:\\stud\\student.ini");</FONT></P>
<P><FONT style="FONT-SIZE: 14px" size=3>  执行后 strStudName 
的值为:"张三",若前两个参数有误,其值为:"默认姓名".</FONT></P>
<P><FONT style="FONT-SIZE: 14px" size=3>  3.读入整型值要用另一个WINAPI函数:</FONT></P>
<P><FONT style="FONT-SIZE: 14px" size=3>UINT GetPrivateProfileInt(<BR>LPCTSTR 
lpAppName,<BR>LPCTSTR lpKeyName,<BR>INT nDefault,<BR>LPCTSTR 
lpFileName<BR>);</FONT></P>
<P><FONT style="FONT-SIZE: 14px" size=3>  这里的参数意义与上相同.使用方法如下:</FONT></P>
<P><FONT style="FONT-SIZE: 14px" 
size=3>nStudAge=GetPrivateProfileInt("StudentInfo","Age",10,"c:\\stud\\student.ini");</FONT></P>
<P><BR><FONT style="FONT-SIZE: 14px" 
size=3>三.循环写入多个值,设现有一程序,要将最近使用的几个文件名保存下来,具体程序如下:</FONT></P>
<P><FONT style="FONT-SIZE: 14px" size=3>  1.写入:</FONT></P>
<P><FONT style="FONT-SIZE: 14px" size=3>CString strTemp,strTempA;<BR>int 
i;<BR>int nCount=6;<BR>file://共有6个文件名需要保存<BR>for(i=0;i 
{strTemp.Format("%d",i);<BR>strTempA=文件名;<BR>file://文件名可以从数组,列表框等处取得.<BR>::WritePrivateProfileString("UseFileName","FileName"+strTemp,strTempA,<BR>"c:\\usefile\\usefile.ini");<BR>}<BR>strTemp.Format("%d",nCount);<BR>::WritePrivateProfileString("FileCount","Count",strTemp,"c:\\usefile\\usefile.ini");<BR>file://将文件总数写入,以便读出.</FONT></P>
<P><FONT style="FONT-SIZE: 14px" size=3>  2.读出:</FONT></P>
<P><FONT style="FONT-SIZE: 14px" 
size=3>nCount=::GetPrivateProfileInt("FileCount","Count",0,"c:\\usefile\\usefile.ini");<BR>for(i=0;i 
{strTemp.Format("%d",i);<BR>strTemp="FileName"+strTemp;<BR>::GetPrivateProfileString("CurrentIni",strTemp,"default.fil", 
strTempA.GetBuffer(MAX_PATH),MAX_PATH,"c:\\usefile\\usefile.ini");</FONT></P>
<P><FONT style="FONT-SIZE: 14px" size=3>file://使用strTempA中的内容.</FONT></P>
<P><FONT style="FONT-SIZE: 14px" size=3>}</FONT></P>
<P><FONT size=3><FONT 
style="FONT-SIZE: 14px">  补充四点:<BR>   1.INI文件的路径必须完整,文件名前面的各级目录必须存在,否则写入不成功,该函数返回 
FALSE 值.<BR>   2.文件名的路径中必须为 \\ ,因为在VC++中, \\ 才表示一个 \ 
.<BR>   3.也可将INI文件放在程序所在目录,此时 lpFileName 参数为: 
".\\student.ini".</FONT></FONT></P>
<P><A id=articleTitle><FONT 
size=3><STRONG>vc操作常用文件操作(三)&nbsp;CFile&nbsp;Example</STRONG></FONT></A> </P>
<P><FONT size=3>CFile is the class used for handling Files in MFC. This class 
can be used for creating, reading, writing and modifying files. It directly 
provides unbuffered, binary disk input/output services, and it indirectly 
supports text files and memory files through its derived classes.</FONT></P>
<H2 tgdmy="0" grllw="0"><FONT 
style="FONT-SIZE: 16px; FONT-FAMILY: Times New Roman">CFile - Creating a 
File:</FONT></H2>
<P tgdmy="0" grllw="0"><FONT 
style="FONT-SIZE: 16px; FONT-FAMILY: Times New Roman">&nbsp;&nbsp; There are two 
ways of creating files. One way is to instantiate the CFile object with the file 
path. This creates the file. The second way is to call the Open function. This 
also creates the file.<BR><BR>CFile cfile_object( "</FONT><A 
href="file:///c://test//codersource_cfile_example.txt" tgdmy="0" grllw="0"><FONT 
style="FONT-SIZE: 16px; FONT-FAMILY: Times New Roman" 
color=#002c99>c:\\test\\codersource_cfile_example.txt</FONT></A><FONT 
style="FONT-SIZE: 16px; FONT-FAMILY: Times New Roman">", 
CFile::modeCreate|CFile:: modeReadWrite);<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp; CFile 
cfile_object;<BR>&nbsp;&nbsp;&nbsp;&nbsp; cfile_object.Open( "</FONT><A 
href="file:///c://test//codersource_cfile_example.txt" tgdmy="0" grllw="0"><FONT 
style="FONT-SIZE: 16px; FONT-FAMILY: Times New Roman" 
color=#002c99>c:\\test\\codersource_cfile_example.txt</FONT></A><FONT 
style="FONT-SIZE: 16px; FONT-FAMILY: Times New Roman">",&nbsp; 
CFile::modeCreate|CFile:: modeReadWrite);<BR>&nbsp;&nbsp;&nbsp;<BR>&nbsp;&nbsp; 
The first parameter to both the functions (CFile() constructor and Open()) is 
the physical path of the file in the disk.&nbsp;The second parameter is an 
enumerated constant. This specifies the mode of opening the file object. The 
above constants&nbsp;modeCreate implies "create a new file" and modeReadWrite 
means "open the file for both reading and writing".</FONT></P>
<P><FONT style="FONT-SIZE: 16px; FONT-FAMILY: Times New Roman" size=2 tgdmy="0" 
grllw="21">&nbsp;&nbsp; If the file is opened without specifying the mode 
constant <STRONG style="FONT-FAMILY: " tgdmy="0" 
grllw="0">shareDenyNone</STRONG>, this file can be opened in read mode 
by&nbsp;other programs. This feature will be necessary for text files, logs 
created by programs. For creating text files we use&nbsp;CFile::typeText and for 
binary files CFile::typeBinary.</FONT></P>
<H2 tgdmy="0" grllw="0"><FONT 
style="FONT-SIZE: 16px; FONT-FAMILY: Times New Roman" size=2 tgdmy="0" 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -