Home > database >  Cant Delete File Unless I Close My Program
Cant Delete File Unless I Close My Program

Time:10-25

I have this code

CreateFileA(path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);

It works perfectly but the only problem is that I cant read, write, or delete the file unless I exit my program. Any Ideas?

CodePudding user response:

You should store the return value of CreateFileA in a variable of type HANDLE:

HANDLE hFile = CreateFileA(path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, nullptr);

Then, when you're done with it, call:

CloseHandle(hFile);

After which you should be able to delete the file.

  • Related