Deleting a file, having problems w compling
Hello, I am pretty new to C++, and I have a question:
I am, and have been using Dev-C++
I used this code, to delete a text file on my desktop. The first time around, it worked perfectly. Given the file example name of "dafile".
#include // needed to use system() function
int main() {
system("del dafile.txt");
return 0;
}
I compiled the file, and ran it, it worked. However, later on, I used the same code, ran it in the same conditions and nothing happened at all.
This system command worked fine the first time around, I have no idea what I have done wrong.
I have tried running this on other computers, in the same conditions, and nothing worked. Has it anything to do with the complied data? There was no errors when I checked it the second time...
Thank you. Any help would be greatly appreciated!
*Sorry about spelling errors*
cosmos22
Junior Poster in Training
80 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
<< Given the file example name of "dafile".
Here the name is dafile
<< system("del dafile.txt");
And here the name is dafile.txt
Do you have a file named 'dafile' on your desktop and you are trying to delete 'dafile.txt', which does not exist?
mitrmkar
Posting Virtuoso
1,809 posts since Nov 2007
Reputation Points: 1,105
Solved Threads: 395
is the file in the same director where the *.exe is located ? If not then your program can't delete it because the program doesn't know where to find it. And you don't have to use the system() function to delete a file -- just use remove () function.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Ah, yes! Thank you, it was the the fact that it was in the wrong area. With this in mind, is it possible to remove a file that is in an UNKNOWN location on the computer according to the .exe. A file that the computer has to find using the scripting?
Thank you...
cosmos22
Junior Poster in Training
80 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
There are win32 api functions that will search for all files and report back to the c program the file names. See FindFirstFile() and FindNextFile() at www.microsoft.com . If that's not what you want then I'm not certain what you mean. Yes your program could search for a specific file all over the hard drive and delete them all, but that would be very very time consuming. There again FindFirstFile() and FindNextFile() will do the job for you.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
I tried using this code:
#include <iostream>
#include <cstdlib>
#include <windows.h>
using namespace std;
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow);
int main()
{
cout <<"Deleting file...";
FileDelete("filedel.txt").
system("del C:\Program Files\filedel.txt");
MessageBox(NULL, "File Deleted!", "Alert!", NULL);
return 0;
}
I had the same problem, where should I declare the FindFirstFile("Location") and where should I place it in the lines of code?
Thank you
cosmos22
Junior Poster in Training
80 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
FindFirstFile() and FindNextFile() works something like the dir DOS command. First call FindFirstFile, then if it returns a valid handle create a loop to call FindNextFile() until no more files are found. There are lots of examples for you to follow if you use google
And BTW you should just delete both lines 14 and 15 if you do the above.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Ok, but what if I wan't to delete A specific file on my computer?
Thanks
cosmos22
Junior Poster in Training
80 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Yes, the command prompt said it was deleting it, but I checked the correct area and it was still there
cosmos22
Junior Poster in Training
80 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
Make sure you have permissions to delete the file and that the file is not marked for read-only.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343