Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I make a program delete and recreate a file? PLEASE HELP!!!!!

Status
Not open for further replies.

TheProgramer

Programmer
Apr 10, 2004
25
US
I just need a snipet of code that will delete and recreate a file. Not Rename, but totaly delete and then create a new file under the same name.

--TheProgramer--
C++ Programer since 2004.
You can contact me at:
theprogramer2004@yahoo.com
 
Doesn't opening the file for writing cause the old contents to be lost?

--
 
Nope...

--TheProgramer--
C++ Programer since 2004.
You can contact me at:
theprogramer2004@yahoo.com
 
If you are using the ofstream class, it will override delete the old content if you use the ios::trunc flag.
 
When using ofstream by default theer are no additional flags required to destroy the old content of a file. Just open.

Ion Filipski
1c.bmp
 
Standard (ANSI/ISO) way to delete a file in C/C++: remove(path) function from <cstdio> (<stdio.h> in C) header.
 
The initial question is:
>How do I make a program delete and recreate a file?

Ion Filipski
1c.bmp
 
You can call ::DeleteFile(path) and then go through the steps to recreate.

Also, CFile or CStdioFile will truncate the file if you specify

CFile::modeCreate WITHOUT the CFile::modeNoTruncate flag

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top