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 <stdlib.h> // 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*

<< 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?

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.

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...

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.

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

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.

Ok, but what if I wan't to delete A specific file on my computer?

Thanks

If you know where that file is then just give it the full path
DeleteFile("C:\Program Files\filedel.txt");

There is no such function as FileDelete()

Yes, the command prompt said it was deleting it, but I checked the correct area and it was still there

Make sure you have permissions to delete the file and that the file is not marked for read-only.

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.