I have thoroughly studied the remove() function documentation, and have gone through a lot of webpages as to how to delete files, but to no avail.
I have one program that is trying to delete a completely separate program (I am making a compiler, and every-time you rebuild the project I need to delete the old ".exe"). However, when I try to delete the file it says that my privileges where denied.
I have made sure that is is NOT read-only, and that the stream was closed before hand... have a look
1 2 3 4 5 6 7
ifstream f((BaseName+".exe").c_str());// Make a file stream
if (f.good()) {// If the file exists
f.close();// Close thew stream
cout << (BaseName+".exe").c_str() << endl;// Show what we are doing
if(remove((BaseName+".exe").c_str())!=0)// Delete the file
perror("\tError deleting file");// Send the error
}// End of if statement
Note:
BaseName = "BOLT Compiler/BOLT"
and the file is called
"BOLT Compiler/BOLT.exe"
SO, if any of you has any insight that could help me, i would be very apreciative :)
what kind of compiler is it? is it for an existing language or are you making one? sorry for being instrusive, but im making one too for a language i made called jade and would love to compare code. i just finished the lexer. also what are you using to learn or used to learn? i have the purple dragon book for reference and im writing the code along side one of udacitys cs course on building an interpreter for a web browser
(Is the assumption you're running on Windows right? Assuming it is...)
I take it that the "exe" you're trying to delete was created by the same program earlier. If so, I don't really see how UAC would fit in: if it has the right to create the file then it will have the right to delete it, unless you altered the file security settings in between. But you could always use Explorer's Properties dialog to check the Security settings for the file and the folder it's in, if you haven't already done so.
My program is not the porgram that created the executable. When my program runs it follows this outline:
The user runs my program to compile their code
My program compiles their code into assembly
FASM (an assembler) turns that code into an executable
// The user recompiles their code
The user runs my program to compile their code
My program compiles their code into assembly
My program sees that there is already an executable
It tries to delete so that FASM can make another one
... And this is where it fails, the EXE is not deletes, so FASM wont make another one.
And yes, I have checked the files properties, it is not read-only. I can right click it and delete it, so I dont understand why the function does not work.
If anybody can replicate my problem, please tell me, until then I will try running my program on another computer (to see if something is wrong with my system).
Are you sure FSAM isn't holding it open? You're confident that your application isn't but you didn't write FSAM so you don't know what it might be doing. I would make sure that the assembler isn't even running in memory before trying to delete the EXE. The more I think about it the more it fits, I assume those other files you mentioned, the ones you could delete didn't come from the assembler did they?
As a side note, sometimes AV suites can do wonky things with files it is suspicious of.