CFile Overwrite

I am constantly writing text to this one file.  I want to open it up, blank it out, then write out my text.  Right now for some reason it keeps appending it to the end.  I don't really understand why tho.  
Who is Participating?
instead of blank it out delete original file.. you may use CFile::Remove() to delete it as new for writting..

You need to specify the CFile::modeCreate flag.  If test.dat exists, this code will overwrite any contents currently in it.

CFile f;
TCHAR* pFileName = _T("test.dat");

if( !f.Open( pFileName, CFile::modeCreate | CFile::modeWrite, NULL ) )
  // Problem; could not open the file

ie. every time if you wish to write text to samefile name.. then you may delete original file  using CFile::Remove() or you may rename original file using CFile::Rename() and your may open new file of same original name for writting.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.