why file remove is not working ?

Apr 2, 2013 at 2:59pm
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;
	}
}
Apr 2, 2013 at 3:17pm
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.
Apr 2, 2013 at 3:36pm
of course, the file is exist
Apr 2, 2013 at 3:43pm
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?

Apr 2, 2013 at 3:44pm
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.
Apr 2, 2013 at 3:56pm
Also you may be able to print the errno variable to help determine why this call failed.

Apr 2, 2013 at 4:01pm
i don't know what the problem is bcz i don;t get error message only -1
Apr 2, 2013 at 4:06pm
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?
Apr 2, 2013 at 6:52pm
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 Apr 2, 2013 at 8:05pm
Apr 2, 2013 at 8:38pm
i found the problem.
i forgot to close the file before delelting it.

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