CFile Overwrite

Posted on 2006-05-19
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
    LVL 22

    Accepted Solution

    instead of blank it out delete original file.. you may use CFile::Remove() to delete it as new for writting..

    LVL 19

    Expert Comment

    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

    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Title # Comments Views Activity
    repeateFront java challenge 31 80
    Unix / Linux grid computing 5 96
    Separate into files by filename 12 60
    wait notify demo infinite loop 3 67
    In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
    Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
    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.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    760 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

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now