CFile::modeNoTruncate

来源:互联网 发布:矩阵在密码学中的应用 编辑:程序博客网 时间:2024/05/01 22:40

CFile::modeNoTruncate 参数详细说明  

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();

参考网站:http://blog.csdn.net/shuilan0066/article/details/5809941
原创粉丝点击