Share via


How to delete the existing file in the first opening of fopen ?

Question

Monday, May 4, 2009 9:06 AM

Hi guys.

For part of my program I am using the function fopen in the appendix mode :

FILE *f=fopen(FileName,"a");

For each measurement I need to delete the old file aand run the program again.
I need away to do that automatically so that in thie first opening (which is triggered by running the program)
it frist deletes the old file and then does the rest the job. How can I do that?

Also, can you come up with other solutions ex. to differetiate the files instead of deleteing like numerating the files created?

Thanks for your help.
looking forward to hearing from you.

All replies (3)

Monday, May 4, 2009 9:17 AM âś…Answered | 1 vote

Hello Yade,

See contents from MSDN for fopen -
"w" - Opens an empty file for writing. If the given file exists, its contents are destroyed .

So the code snippet should be

FILE *f=fopen(FileName,"w");

Regards,
Jijo.http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.


Monday, May 4, 2009 9:23 AM

Maybe it is enough to delete the file explicitly using _unlink(FileName) at the beginning of your program? The rest of multiple fopens will append information to cleared file.


Monday, May 4, 2009 10:46 AM

The remove() function might be a better choice than _unlink, as it is Standard C/C++ and ergo more portable.

  • Wayne