如果不存在,则需要创建一个文件 [英] Need to create a file if it does not exist
问题描述
大家好,
我有一个查询,我需要编写代码,这样我就需要打开一个文件(如果该文件不存在),则应首先创建然后打开.我这样写的
CStdioFile文件;
file.Open(_strDestination,CFile :: modeReadWrite | CFile :: typeText);
我是这样写的,但没有帮助.
在此先感谢您.
Hi all ,
I have a query , i need to write a code such that I need to open a file if it does not exist it should first create and then open . I wrote this way
CStdioFile file;
file.Open(_strDestination,CFile::modeReadWrite | CFile::typeText);
I wrote it this way but did not help .
Thanks in advance .
推荐答案
CStdioFile file;
file.Open(_strDestination, CFile::modeReadWrite | CFile::typeText | CFile::modeCreate | CFile::modeNoTruncate );
CFile :: modeNoTruncate将此值与modeCreate结合在一起.如果正在创建的文件已经存在,则不会将其截短为0.此外,如果正在创建的文件已经存在,则基础文件指针将指向文件的开头.该标志保证文件可以以新创建的文件或现有文件的形式打开.例如,当打开可能不存在的设置文件时,这可能会很有用.此选项也适用于CStdioFile.
CFile::modeNoTruncate Combine this value with modeCreate. If the file being created already exists, it is not truncated to 0 length. Furthermore, if the file being created already exists, the underlying file pointer will point to the beginning of the file. This flag guarantees the file 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.
这篇关于如果不存在,则需要创建一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!