I've made a program that creates a log of the activities performed in it, for easy reference for the user. But I want the program to automatically delete the file after the program is shut down. What is the syntax and how do I go about doing it?

Recommended Answers

All 14 Replies

To delete a file use the C++ remove() function.

If you're writing a Windows application, try the DeleteFile function available in winbase.h:

BOOL DeleteFile(
  LPCTSTR lpFileName   // pointer to name of file to delete

the program was written to run in command prompt/dos, and I still can't get it to work. any other suggestions?

Say I tried to do it with windows, where would I specify the file I wanted to be deleted with the syntax you just gave me?

Oh, goodness gracious! I guess it would be most appropriate to make this my 667th post then.

Are those suppose to be horns? They look like donkey ears.

An easy way to delete a file is to just call a system command from Windows. dafile.txt is the file you want to delete.

#include <stdlib.h> // needed to use system&#40;&#41; function

int main&#40;&#41; &#123;
     system&#40;"del dafile.txt"&#41;;		
     return 0;

Ebil cscgal :twisted:

Anyway, how would I do that so it deleted the file at closing?

What do you mean at closing? When you close your program? If so, just make it one of your last lines.

Use the remove function ..ok BOB.. but how do i use it ,, syntax etc...
please bob, it's an emergancy.

In header <cstdio>:
int remove(const char* filename)

Probably too late for you by now, as you needed an answer urgently. I hope you had the sense to look up remove() in your C++ book, or do a Google search.

Ebil cscgal :twisted:

Anyway, how would I do that so it deleted the file at closing?

you can register a function that will be called when the program terminates with the atexit() function. this func could tehn call remove() on the file.

what is the difference between remove and del ?

Go easy on me. I'm trying to teach myself c++ and its a slow go for me. Just me, my book, and Google.

FYI... im wanting to write a program that goes through and deletes files of a certain extension. (sort of a janitor utility)

some proprietary software makes these files and will eventually fill up a hard drive. i just want a simple program I can run to clean the directory.


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.