欢迎访问:常州市武进区嘉泽中心小学网站 !今天是:
栏目列表
您现在的位置是:首页>>教师>>计算机技术>>程序设计>>杂项>>文章内容
VC++中以追加方式向文本文件写入数据
发布时间:2008-11-20   点击:   来源:本站原创   录入者:佚名
 

VC++中以追加方式向文本文件写入数据

 

在VB、Asp中向文本文件追加数据很容易,只要设定一个参数为ForAppending就行了。

Sub OpenTextFileTest

   Const ForReading = 1, ForWriting = 2, ForAppending = 8

   Dim fso, f

   Set fso = CreateObject("Scripting.FileSystemObject")

   Set f = fso.OpenTextFile("c:\testfile.txt", ForWriting, True)

   f.Write "Hello world!"

   f.Close

End Sub


在c语言中,追加数据也比较简单,好像设定a+参数就可以了。

 

今天,我要用MFC中的CStdioFile类进行文件操作,读写等。

可是,看了下好像没有简单的方法,

于是在网上看到这样的写法:

CStdioFile file(strFile,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite);

file.WriteString(strTmp);

file.Close;


modeNoTruncate的意思就是不要截取的意思吧

可是,试了下这段代码,并没有起作用,不知道是什么原因。

于是,在WriteString写字符串之前加了个把指针先定位到文件末尾的代码,就可以了

CString strTmp="hehe\r\n";
 
CStdioFile file(strFile,CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite);

file.SeekToEnd();//先定位到文件尾部

file.WriteString(strTmp);

file.Close;

 

 


附件:
    关闭窗口
    打印文档
    账号登录
    保持登录 忘记密码?
    账号与武进教师培训平台同步