2

I was trying the DeleteFile() function, and I wrote the program below.

#include <iostream>
#include <fstream>
#include <windows.h>
using namespace std;
int main(){
    FILE * filetxt;
    // creat a file
    filetxt = fopen("C:\\Users\\Thomas\\Desktop\\filetxt.txt", "w");

    // delete the file 
    if (DeleteFile("\\\\.\\C:\\Users\\Thomas\\Desktop\\filetxt.txt") != 0){
        cout<<"success";
    }else{
        cout<<"fail";
    }
    cin;
}

But the program didn't work as it was supposed to. The file created wasn't deleted.

The output is:

fail
eerorika
  • 232,697
  • 12
  • 197
  • 326

1 Answers1

10

You opened the file with fopen and you called DeleteFile before closing it with fclose.

As you can read from DeleteFile MSDN documentation:

The DeleteFile function fails if an application attempts to delete a file that has other handles open for normal I/O or as a memory-mapped file (FILE_SHARE_DELETE must have been specified when other handles were opened).

Note also that, on failure, you can call GetLastError after DeleteFile to get an error code with more information about the cause of the failure.

Mr.C64
  • 41,637
  • 14
  • 86
  • 162
  • Answering a question about a sharing violation without even mentioning sharing is incomplete at best. You don't *have* to close a file before deleting it. You just have to make sure that it is opened using an appropriate sharing mode. Not opening a file is equivalent to sharing it for anything, but that's just a special case of the more generic issue: Making sure to not provoke a sharing violation. – IInspectable Jan 27 '17 at 13:13
  • @IInspectable Instead of giving judgements like "wildly incomplete" to other people's answers, I encourage you to write your own answer. Wait, [I've found an answer of yours](http://stackoverflow.com/a/19624153/1629821) on converting from std::string to CString in Unicode projects that doesn't mention code pages and encoding for the std::string. I guess it's still "wildly incomplete", but whatever... – Mr.C64 Jan 27 '17 at 15:12
  • Ah dammit. Gotta -1 this one, just because it's you. – IInspectable Jan 27 '17 at 16:08
  • @IInspectable Ah dammit. I returned the favor on your other answer, just because it's you. You are feee to down-vote every answer of mine, but I still think this one (while not perfect) is a valid good answer that does NOT deserve down-votes. Stackoverflow will be a better community when people start to understand to be more humble and more respectful of other people's efforts and time. – Mr.C64 Feb 01 '17 at 15:57
  • Be humble in accepting that [voting is important](http://stackoverflow.com/help/why-vote), even if you don't like it. – IInspectable Feb 01 '17 at 16:48
  • I do agree that _serious_ voting is important. An answer can be up voted, down voted, or none of these. I think down voting is for really bad answers, and I think this one is not. As I said, it's not perfect, but I believe it doesn't deserve a down vote. Anyway, as I already wrote, you are free to down vote every answer of mine, if you wish :) I won't return the favor, as I have better things to do, and I also think you contributed good answers that deserve up votes. That said, if you really think this answer deserves a down vote, I respect your right to act accordingly. – Mr.C64 Feb 04 '17 at 16:41