CFile also supports getting file status, including whether the file exists, creation and modification dates and times, logical size, and path.
Thus, you could use the result of GetStatus to determine whether to use the CFile::modeCreate flag when opening a file, as shown by the following example:
CFile theFile;
char* szFileName = "c:\\test\\myfile.dat";
BOOL bOpenOK;
CFileStatus status;
if( CFile::GetStatus( szFileName, status ) )
{
// Open the file without the Create flag
bOpenOK = theFile.Open( szFileName,
CFile::modeWrite );
}
else
{
// Open the file with the Create flag
bOpenOK = theFile.Open( szFileName,
CFile::modeCreate | CFile::modeWrite );
}
For related information, see Serialization (Object Persistence).