954,119 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Syntax for deleting specified file ( in C++)

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?

Mr Gates
Light Poster
36 posts since May 2003
Reputation Points: 13
Solved Threads: 0
 

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
);
cscgal
The Queen of DaniWeb
Administrator
19,421 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 229
 

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?

Mr Gates
Light Poster
36 posts since May 2003
Reputation Points: 13
Solved Threads: 0
 

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

Bob
Junior Poster
Team Colleague
129 posts since Feb 2003
Reputation Points: 15
Solved Threads: 2
 

thanks Bob :)

cscgal
The Queen of DaniWeb
Administrator
19,421 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 229
 

You're welcome.

Now, please post again. That 666 (posts) is making me uneasy ;-)

[img]http://www.robertjacobs.fsnet.co.uk/images/dani2.jpg[/img]

Bob
Junior Poster
Team Colleague
129 posts since Feb 2003
Reputation Points: 15
Solved Threads: 2
 

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

cscgal
The Queen of DaniWeb
Administrator
19,421 posts since Feb 2002
Reputation Points: 1,474
Solved Threads: 229
 

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

samaru
a.k.a inscissor
Team Colleague
1,256 posts since Feb 2002
Reputation Points: 262
Solved Threads: 18
 

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;
&#125;
samaru
a.k.a inscissor
Team Colleague
1,256 posts since Feb 2002
Reputation Points: 262
Solved Threads: 18
 

Ebil cscgal :twisted:

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

Mr Gates
Light Poster
36 posts since May 2003
Reputation Points: 13
Solved Threads: 0
 

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

samaru
a.k.a inscissor
Team Colleague
1,256 posts since Feb 2002
Reputation Points: 262
Solved Threads: 18
 

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

sixtOnePercent
Newbie Poster
1 post since Jan 2004
Reputation Points: 10
Solved Threads: 0
 

In header :
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.

Bob
Junior Poster
Team Colleague
129 posts since Feb 2003
Reputation Points: 15
Solved Threads: 2
 
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.

infamous
Junior Poster in Training
77 posts since Mar 2004
Reputation Points: 47
Solved Threads: 2
 

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.

Thanks

brainysmurf0316
Newbie Poster
1 post since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You