Chinaunix首页 | 论坛 | 博客
  • 博客访问: 494811
  • 博文数量: 96
  • 博客积分: 6046
  • 博客等级: 准将
  • 技术积分: 908
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-07 22:40
文章分类

全部博文(96)

文章存档

2009年(12)

2008年(18)

2007年(45)

2006年(21)

我的朋友

分类: C/C++

2007-06-25 14:09:55

在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;

阅读(5938) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~