Hello. I've been doing some programming recently that is supposed to delete files from a specified pathway

#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...";
    // ??? What to do here?
    MessageBox(NULL, "File Deleted!", "Alert!", NULL);
    return 0;
}

As indicated above, I don't really know how to introduce the delete function here. I would like to delete a file from a certain pathway on the computer, e.g.

("C:\Program Files\filedel.txt"); How would I run this, so it can locate the file itself, witout the aid of running the program in the same folder or location to insure it's deleted anyway.


Thank you, help is appreciated!

Recommended Answers

All 21 Replies

First of all, you have there two 'main' functions, one for a windows graphical interface application and one for a console application. You only can have one of them, not both. I think you can remove the WinMain() function altogether and use just the 'int main()'.

Actually the main function can be written like:

int main(int argc, const char * argv[])

-argc is number of the arguments that were given to the program
-argv[] is an array holding the arguments that were given to the program

Try out the following small program to see how you can use the arguments. Test it like:
myprog.exe somearg1 somearg2

#include <iostream>
int main(int argc, const char * argv[])
{
       // display all program arguments that it received via command line
	for(int nn = 0; nn < argc; nn ++)
	{      // the first one, at index 0, is actually the program itself, always
		cout << "program argument: #" << nn << ": " << argv[nn] << endl;
	}
        return 0;
}

If you make use of the arguments, you can code your application so that it is given the file to delete as an argument i.e. on command line:
mydelete.exe c:\temp\somefile.txt

Since you are obviously coding on Windows, you can use the DeleteFile() function that is built-in to Windows. It takes only one argument, namely the file path to delete.
Below is one solution ... you can modify it to suit your needs.

int main(int argc, const char * argv[])
{
       // check that we were given one argument only (the file to delete)
	if(2 != argc)
	{
		cout << "Error: exactly one program argument accepted." << endl;
		return 0;
	}

	// Get a pointer to the file name/path
	const char * pFileToDelete = argv[1];

	// try deleting it using DeleteFile
	if(DeleteFile(pFileToDelete))
	{
		// succeeded
		cout << "Deleted file: " << pFileToDelete << endl;
	}
	else
	{
		// failed
		cout << "Failed to delete the file: " << pFileToDelete << endl;
	}
	return 0;
}

I see, do I then replace pFileToDelete with something like pTextdocument.txt? Thank you in advance...

Unfortunetly, I compiled this, and it highlighted this area:

cout << "Error: exactly one program argument accepted." << endl;

It stated the function was undeclared...

Could you help please?


Thank you again...

I see, do I then replace pFileToDelete with something like pTextdocument.txt?

With something like: "c:\\mydirectory\\myfile.txt" (please note the double backslashes and double quotes)

Unfortunetly, I compiled this, and it highlighted this area:

cout << "Error: exactly one program argument accepted." << endl;

replace it with: std::cout << "Error: exactly one program argument accepted." << std::endl; Or you could add the line using namespace std; to your source. (between #include <iostream>and int main() )

Niek

How would I declare the delete file function, (as I'm getting an error message) and if I wanted to delete from a file directory, would I use something like this>:

if(DeleteFile(c:\\My Programs\\myfile.txt))


Thank you


Oh, and i tried this, and it gave me this error message:

expected primary-expression before const

Thanks again.

How would I declare the delete file function,

just #include <windows.h> If you don't have that installed on your computer then you can't use that function. Use the standard C function remove() instead.

i tried this, and it gave me this error message:

expected primary-expression before const

my code on the line of error

const char * "c:\\My Programs\\myfile.txt" = argv[1];

Thanks again.


Sorry about double message, I just need to check

first of all: what Ancient Dragon said

then:

if(DeleteFile(c:\\My Programs\\myfile.txt))

Allmost. You should use double quotes as I said earlier:

if(DeleteFile("c:\\My Programs\\myfile.txt")) 
    std::cout << "succes" << std::endl;
else 
    std::cout << "failed" << std::endl;

use header #include <windows.h> [edit]
and syntax example:
DeleteFile(TEXT("datafile.txt"));

its for vc++;

my code on the line of error

const char * "c:\\My Programs\\myfile.txt" = argv[1];

That line is malformed. Should be either

const char * pFileToDelete = "c:\\My Programs\\myfile.txt";

or
const char * pFileToDelete = argv[1];

Use argv[1] line if you want to type tye filename on the command line when you execute your program. Otherwise if you just want to hard-code the filename into your program use the first version.

Any ideas what is wrong with this?


const char * pFileToDelete = argv[1];

changed to

const char * "c:\\My Programs\\myfile.txt" = argv[1];

Thanks

use header #include <stdio.h>
and syntax example:
DeleteFile(TEXT("datafile.txt"));

its for vc++;

correction: It's for C++ with UNICODE enabled.

And as far as I know, DeleteFile is declared in WinBase.h, so you would still have to include Windows.h.

const char * "c:\\My Programs\\myfile.txt" = argv[1];

That's not what I meant. Just try my code and see if it works. You will have to change : "c:\\My Programs\\myfile.txt" to the path of the file you want to delete.

Question: Is it a requirement that you give the name of the file to delete as a parameter to your program? Like this: yourprogram.exe test.txt Niek

I ran this but nothing happened, what do I have to do to make this work?

#include <iostream>
#include <windows.h>
int main(int argc, const char * argv[])
{
// check that we were given one argument only (the file to delete)
if(2 != argc)
{
std::cout << "Error: exactly one program argument accepted." << std::endl;
return 0;
}


// Get a pointer to the file name/path
const char * pFileToDelete = "c:\\My Programs\\myfile.txt";


// try deleting it using DeleteFile
if(DeleteFile("c:\\My Programs\\myfile.txt"))
{
// succeeded
std::cout << "Deleted file: " <<  std::endl;
}
else
{
// failed
std::cout << "Failed to delete the file: " << std::endl;
}
return 0;
}

I ran this but nothing happened, what do I have to do to make this work?

Remove these lines:

// check that we were given one argument only (the file to delete)
if(2 != argc)
{	
    std::cout << "Error: exactly one program argument accepted." << std::endl;
    return 0;
}

These lines are checking if you call your program with extra arguments, which you are not.

Then replace : if(DeleteFile("c:\\My Programs\\myfile.txt")) with: if(DeleteFile(pFileToDelete )); And it should work

Niek

#include <iostream>
#include <windows.h>
int main(int argc, const char * argv[])
{


	// Get a pointer to the file name/path
	const char * pFileToDelete = "c:\\Program Files\\myfile.txt";

	// try deleting it using DeleteFile
	if(DeleteFile(pFileToDelete ));

	{
		// succeeded
		std::cout << "Deleted file: " <<  std::endl;
	}
	else
	{
		// failed
		std::cout << "Failed to delete the file: " << std::endl;
	}
	return 0;
}

I'm sorry, I have no idea where the brackets should be ending and starting here, it says that there is an expected-primary expression before else.


Thanks guys

if(DeleteFile(pFileToDelete ));

remove ;(semi colon)

You're allmost there:

if(DeleteFile(pFileToDelete ));

You should remove the semicolon at the end of the line:

if(DeleteFile(pFileToDelete ))

And don't forget to #include <iostream>

Niek

Thank you guys so much!! That's awesome!

I appreciate all your help, and if I have any other questions to ask about C++, I'll definitely come here

:)

The only thing I would ask, is the boxes don't come up, it just does its own thing. Apart from that, its all good.

add a std::cin.get(); just before return 0;

Thank you!

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.