CFile Overwrite

Posted on 2006-05-19
Medium Priority
Last Modified: 2013-11-20
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.  
Question by:cophi
  • 2
LVL 22

Accepted Solution

mahesh1402 earned 2000 total points
ID: 16717399
instead of blank it out delete original file.. you may use CFile::Remove() to delete file.....open it as new for writting..

LVL 19

Expert Comment

ID: 16717402
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

LVL 22

Expert Comment

ID: 16717420
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.


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question