remove() is not working :(

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 :)
BTW, i can delete any other file in the folder... just not that one
Have you checked 'errno'?: http://www.cplusplus.com/reference/cerrno/errno/

Have you tried pre-pending the full path to the file?
perror says i dont have permission, and yes, full and short paths dont work
Have you tried TEMPORARILY turning off UAC? Or maybe running the executable as Admin.
Are you trying to delete the same executable that is running btw ?
modoran wrote:
Are you trying to delete the same executable that is running btw ?
NanoBytes wrote:
I have one program that is trying to delete a completely separate program

Guess not.
Probably a UAC thing.
I understand if you want to keep your code flexible but for diagnostic purposes, does 'DeleteFile()' return a different error?:http://msdn.microsoft.com/en-us/library/windows/desktop/aa363915(v=vs.85).aspx

Or did you try running your program alone as administrator?
closed account (Dy7SLyTq)
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
Last edited on
(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.

You could also check with handle.exe to see if something is still holding a lock on the file.
http://technet.microsoft.com/en-gb/sysinternals/bb896655.aspx

Andy

PS I assume you have tried the code fragment you posted in a stabd alone app to check it in isolation (it looks fine to me.)
Last edited on
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.
Ah, that is a very good idea. That actually make a lot of sense, thank you
Topic archived. No new replies allowed.