why file remove is not working ?

I try to remove file and ut not working.
it's a file that i created in the program and i try to remove it. but i'm getting -1 from function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void PhysicalFile::pdelete()
{
	opened = false;

	string track = WorkingDir + FileName + ".hash";

	if (remove(track.c_str()) != 0)
		throw exception ("Error deleting file\n");
	else
	{
		openmode = "I";	
		WorkingDir = "";
		FileName = "";			
		FileSize = 0;
	}
}
The first question I have is, are you sure that the file actually exists? I would recommend you actually pass the file name into this function.
of course, the file is exist
How did you test that assumption? Did you print out your variable track to insure it is correct?

Another question, are you sure you have permission to delete the file?

Are you sure the file is not currently open in another part of your program?

But, for example, what is the value of WorkingDir? Could this be different to the actual file location?

At the very least, I'd output the value of track in the error message.

Also, is the file still open or in use at the time.
Also you may be able to print the errno variable to help determine why this call failed.

i don't know what the problem is bcz i don;t get error message only -1
Did you read the documentation for remove() : http://www.cplusplus.com/reference/cstdio/remove/?

Did you read the following lines:
If the file is successfully deleted, a zero value is returned.
On failure, a nonzero value is returned.
On most library implementations, the errno variable is also set to a system-specific error code on failure.

Did you follow the link for perror() (introduced in the example)?: http://www.cplusplus.com/reference/cstdio/perror/

Did you try printing out the error information using this function?

What exactly is the value of track?
thak you, now i'm getting the error
Error deleting file q:\yoni.hash : Permission denied

and i have no idea why?

i thought it's locked but if i try to delete it from explorer it's delete
Last edited on
i found the problem.
i forgot to close the file before delelting it.

thanx to all helpers.
Topic archived. No new replies allowed.