📄 vc中常用文件操作(转载) - yfwei的专栏 - csdnblog.htm
字号:
grllw="21">CFile - Writing to a File:</FONT></H2>
<P tgdmy="0" grllw="12"><FONT
style="FONT-SIZE: 16px; FONT-FAMILY: Times New Roman"> The function
Write is used to write data to the files. The sample code is as
follows.<BR><BR> CFile
cfile_object;<BR> cfile_object.Open(
"c:\\test\\codersource_cfile_example.txt",
CFile::modeCreate|CFile::modeWrite);<BR><BR> char
szSampleText[100];<BR> strcpy(szSampleText, "Sample Text
for CFile Write function Example");<BR>
cfile_object.Write (szSampleText,100); <BR></FONT></P>
<P tgdmy="0" grllw="0"><FONT
style="FONT-SIZE: 16px; FONT-FAMILY: Times New Roman"> If there is
any need to write text line by line, it is better to use the class
CStdioFile.<BR></FONT></P>
<H2 tgdmy="0" grllw="0"><FONT
style="FONT-SIZE: 16px; FONT-FAMILY: Times New Roman">CFile - Reading from a
file:</FONT></H2>
<P tgdmy="0" grllw="0"><FONT
style="FONT-SIZE: 16px; FONT-FAMILY: Times New Roman"> The function
Read is used to read data from files. The sample code
is,<BR><BR> CFile
cfile_object;<BR> cfile_object.Open(
"c:\\test\\codersource_cfile_example.txt",
CFile::modeCreate|CFile::modeWrite);<BR><BR> char
szSampleText[100];<BR> UINT lBytesRead =
cfile_object.Read (szSampleText,100); </FONT></P>
<P tgdmy="0" grllw="0"><FONT
style="FONT-SIZE: 16px; FONT-FAMILY: Times New Roman"> The function
returns the number of bytes read from the file. The maximum number of characters
read will be the second parameter of the Read function.</FONT></P>
<H2 tgdmy="0" grllw="0"><FONT
style="FONT-SIZE: 16px; FONT-FAMILY: Times New Roman">CFile - closing the
file:</FONT></H2>
<P tgdmy="0" grllw="0"><FONT
style="FONT-SIZE: 16px; FONT-FAMILY: Times New Roman"> The Close
function is used to close the file. But the close function need not be called,
as the destructor will automatically call it if the file is open. So when
the object goes out of scope, the destructor calls close
function.</FONT><BR><BR><FONT
style="FONT-SIZE: 16px; FONT-FAMILY: Times New Roman">from:</FONT> <A
href="http://www.codersource.net/mfc_cfile.html"><FONT
style="FONT-SIZE: 16px; FONT-FAMILY: Times New Roman"
color=#333333>http://www.codersource.net/mfc_cfile.html</FONT></A></P>
<DIV></DIV>
<DIV><A id=articleTitle><FONT
size=3><STRONG>vc中常用文件操作(四)CStdioFile</STRONG></FONT></A><STRONG>
</STRONG></DIV>
<DIV><STRONG>CFile</STRONG>的派生类CStdioFile提供了对文件进行流式的操作功能。其中函数void
CStdioFile::WriteString( LPCTSTR lpsz )写入一个字符串,需要给字符串lpsz的末尾加上换行标志”\r\n”;函数bool
CStdioFile::ReadString(CString &rString )从文件中读取一行,如果文件未读完返回true,否则返回false。
<P align=left><FONT style="FONT-SIZE: 14px">比如:写入文件的例子</FONT></P>
<P align=left><FONT style="FONT-SIZE: 14px">//创建文件</FONT></P>
<P align=left><BR><FONT style="FONT-SIZE: 14px">CStdioFile
file;<BR>file.Open("ts.txt",CFile::modeCreate|CFile::modeWrite);</FONT></P>
<P align=left><FONT style="FONT-SIZE: 14px">//写入文件</FONT></P>
<P align=left><FONT style="FONT-SIZE: 14px">CString
str;<BR>str.Format("%s\r\n","hello!I am
talkingmute!");<BR>file.Seek(0,CFile::end);<BR>file.WriteString( str
);</FONT></P>
<P align=left><FONT style="FONT-SIZE: 14px">//关闭文件</FONT></P>
<P align=left><FONT style="FONT-SIZE: 14px">file.Close();</FONT></P>
<P align=left><FONT style="FONT-SIZE: 14px">比如:读文件的例子</FONT></P>
<P align=left><FONT style="FONT-SIZE: 14px">CString strText = “”;<BR>CString
szLine = “”;</FONT></P>
<P align=left><FONT style="FONT-SIZE: 14px">//打开文件<BR>CStdioFile
file;<BR>file.Open("ts.txt",CFile::modeRead);</FONT></P>
<P align=left><FONT style="FONT-SIZE: 14px">//逐行读取字符串<BR>while( file.ReadString(
szLine ) )<BR>{<BR> strText += szLine;<BR>}</FONT></P>
<P align=left><FONT style="FONT-SIZE: 14px">MessageBox(strText);</FONT></P>
<P align=left><FONT style="FONT-SIZE: 14px">//关闭文件</FONT></P>
<P align=left><FONT style="FONT-SIZE: 14px">file.Close();<BR></FONT></P>
<P id=TBPingURL><FONT style="FONT-SIZE: 14px">Trackback:
http://tb.blog.csdn.net/TrackBack.aspx?PostId=15737</FONT>74</P></DIV></DIV>
<P class="right articalinfo">发表于 @ <A title=permalink
href="http://blog.csdn.net/yfwei/archive/2007/09/05/1772605.aspx">2007年09月05日
08:24:00</A>|<A title=评论
href="http://blog.csdn.net/yfwei/archive/2007/09/05/1772605.aspx#FeedBack">评论(<SPAN
id=FeedbackCount_1772605>loading...</SPAN>
<SCRIPT type=text/javascript>AddFeedbackCountStack("1772605")</SCRIPT>
)</A>|<A title=编辑
href="http://writeblog.csdn.net/PostEdit.aspx?entryId=1772605">编辑</A></P><SPAN
id=Post.ascx_ViewPost_PreviousAndNextEntriesDown>
<H3><A
href="http://blog.csdn.net/yfwei/archive/2007/09/05/1772902.aspx">新一篇: STL中map用法详解(转载)</A> | <A
href="http://blog.csdn.net/yfwei/archive/2007/09/04/1771658.aspx">旧一篇: CString
常用函数(转载)</A></H3></SPAN></DIV></DIV>
<DIV class=commentslist><SPAN id=Anthem_Post.ascx_Comments_ltlComments__><SPAN
id=Post.ascx_Comments_ltlComments>
<DIV id=commentslist>
<H3>评论:没有评论。</H3></DIV></SPAN></SPAN></DIV>
<DIV class=spacecommment>
<DIV id=Anthem_Post.ascx_PostComment_CommentUpdatePanel__>
<DIV id=Post.ascx_PostComment_CommentUpdatePanel>
<FIELDSET><LEGEND>发表评论</LEGEND>
<UL>
<LI>姓 名:<INPUT id=Post.ascx_PostComment_tbName disabled
maxLength=32 size=40 name=Post.ascx:PostComment:tbName><SPAN
id=Post.ascx_PostComment_RequiredFieldValidator2
style="VISIBILITY: hidden; COLOR: red" initialvalue=""
evaluationfunction="RequiredFieldValidatorEvaluateIsValid"
errormessage="请输入名字"
controltovalidate="Post.ascx_PostComment_tbName">请输入名字</SPAN>
<LI>主 页:<INPUT id=Post.ascx_PostComment_tbUrl disabled
maxLength=256 size=40 name=Post.ascx:PostComment:tbUrl>
<LI>
<DIV id=Post.ascx_PostComment_AntiBotImageRegion>校验码: <INPUT
id=Post.ascx_PostComment_tbAntiBotImage style="WIDTH: 75px"
name=Post.ascx:PostComment:tbAntiBotImage> <SPAN
id=Post.ascx_PostComment_valAntiBotImage style="DISPLAY: none; COLOR: red"
evaluationfunction="CustomValidatorEvaluateIsValid"
display="Dynamic">检验码无效!</SPAN> <SPAN
id=Anthem_Post.ascx_PostComment_imgAntiBotImage__><IMG
id=Post.ascx_PostComment_imgAntiBotImage alt=""
src="vc中常用文件操作(转载) - yfwei的专栏 - CSDNBlog.files/AntiBotImage.aspx"
align=absMiddle border=0></SPAN> <SPAN
id=Anthem_Post.ascx_PostComment_lbCreateAntiBotImage__><A
id=Post.ascx_PostComment_lbCreateAntiBotImage
onclick="javascript:Anthem_FireCallBackEvent(this,event,'Post.ascx:PostComment:lbCreateAntiBotImage','',false,'','','',true,null,null,null,true,true);return false;"
href="javascript:__doPostBack('Post.ascx$PostComment$lbCreateAntiBotImage','')">看不清,换一张</A></SPAN>
</DIV></LI></UL><SPAN id=Post.ascx_PostComment_RequiredFieldValidator3
style="VISIBILITY: hidden; COLOR: red" initialvalue=""
evaluationfunction="RequiredFieldValidatorEvaluateIsValid" errormessage="请输入评论"
controltovalidate="Post.ascx_PostComment_tbComment">请输入评论</SPAN><BR><TEXTAREA id=Post.ascx_PostComment_tbComment disabled name=Post.ascx:PostComment:tbComment rows=10 cols=50></TEXTAREA><BR><SPAN
id=Anthem_Post.ascx_PostComment_btnSubmit__></SPAN><BR><SPAN
id=Post.ascx_PostComment_Message
style="COLOR: red">当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击<A
href="http://passport.csdn.net/member/UserLogin.aspx?from=http://blog.csdn.net/yfwei/archive/2007/09/05/1772605.aspx">登录</A></SPAN>
</FIELDSET> </DIV></DIV></DIV>
<SCRIPT type=text/javascript>
LoadFeedbackCount();//加载评论
document.write("<img src='http://counter.csdn.net/pv.aspx?id=24' border=0 width=0 height=0>");// 计数器
</SCRIPT>
<SCRIPT src="vc中常用文件操作(转载) - yfwei的专栏 - CSDNBlog.files/HighLighter.js"
type=text/javascript></SCRIPT>
</DIV></DIV></DIV>
<DIV id=pubfooter>
<DL>
<DT>
<DD>Csdn Blog version 3.1a
<DD>Copyright © yfwei </DD></DL></DIV></DIV>
<SCRIPT type=text/javascript>
<!--
var Page_Validators = new Array(document.getElementById("Post.ascx_PostComment_RequiredFieldValidator2"), document.getElementById("Post.ascx_PostComment_valAntiBotImage"), document.getElementById("Post.ascx_PostComment_RequiredFieldValidator3"));
// -->
</SCRIPT>
<SCRIPT type=text/javascript>
<!--
var Page_ValidationActive = false;
if (typeof(ValidatorOnLoad) == "function") {
ValidatorOnLoad();
}
function ValidatorOnSubmit() {
if (Page_ValidationActive) {
return ValidatorCommonOnSubmit();
}
else {
return true;
}
}
// -->
</SCRIPT>
</FORM>
<SCRIPT src="vc中常用文件操作(转载) - yfwei的专栏 - CSDNBlog.files/counter.js"
type=text/javascript></SCRIPT>
</DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -