CFile::modeNoTruncate 参数详细说明

来源:互联网 发布:淘宝助理关联电子面单 编辑:程序博客网 时间:2024/05/23 00:46

MSDN上说到:
CFile::modeNoTruncate Combine this value with modeCreate. If the file being created already exists, it is not truncated to 0 length. Thus the file is guaranteed to open, either as a newly created file or as an existing file. This might be useful, for example, when opening a settings file that may or may not exist already. This option applies to CStdioFile as well.

  意思是,用modeCreate模式创建和打开一个文件,假如这个文件已经存在,则会清空这个已经存在的文件,加上modeNoTruncate的话,就不会清空这个文件了。举个例子,如果原来文件长80K的话,而你要写50K的数据。用一般的modeCreate,先会把文件长度重置为0,再写入50K数据,最后文件长度为50K用CFile::modeNoTruncate的话,直接写入50K数据,最终文件长度还是80K。数据为前部分50K新数据和后部30K旧数据。

  在MFC中,追加数据也比较简单,好像设定CFile::modeNoTruncate参数就可以了。这个例子在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;

  这几天都在玩MFC的文件操作相关类,前天的CFileStatus,可以看看CFileStatus 使用详解,这些代码都为下个版本的飞鸽传书作准备的,阿翔也正在研究P2P技术的实现,很快飞鸽传书将成为一个强大的即时通讯软件。

 

参考资料:

http://blog.163.com/cyin8@126/blog/static/172796962009727102249186/


如果此文对您有所帮助,还望点击一下以下网站:


    360导航

    2345导航        

   古典小说网,本人的网站
原创粉丝点击