File deletion not working

Hello

I'm trying to delete a simple Text-file, the function returns 1 (successful) but when I look in the Windows File Explorer, the file is still there.

What to do?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
	int deleteFile(string fileToDelete) {
		char *cstr = new char[fileToDelete.length()];
		strcpy_s(cstr, 256, fileToDelete.c_str());

		ifstream fileExistance(cstr);

		if (fileExistance) {
			if (remove(cstr)) {
				return 1;
			} else {
				return 0;
			}
		} else return 3;
	}


Thanks for reading,
Niely
Last edited on
1) remove, like many other C functions, returns 0 (false) on success.
2) You cannot delete open file, and in your codeyou are trying to do exactly that.
^Thanks a lot!
Your first response helped me a lot! ;D
I still have another problem, when I run my application, I sometimes get a Breakpoint error.
What is it, and how to resolve it?
use the condition
1
2
 if(!fileExistance.good())
      return -1;


-1 like everyone do .

Also make sure to check if 256 is long enough for your absolute path (if not relative).

Note also you allocate a char * but dont delete[] it .
Topic archived. No new replies allowed.