C
Coffee Pot
Thanks for any advice.
~ CP
~ CP
Malcolm McLean said:I don't know about Windows specifically but generally you need to
close the file or the attempted deletion will fail.
system("del C:\\path\\to\\file");
will delete.
[...]Nate Eldredge said:[...]Malcolm McLean said:I don't know about Windows specifically but
To keep this semi-on-topic, standard C provides the remove() function.
However the results are "implementation defined" if the file is open, so
to be portable you would need to close it first. I also don't know what
Windows implementations usually do but
I don't know about Windows specifically but generally you need to
close the file or the attempted deletion will fail.
I guess you could do this by appending a line
DEL c:\path\to\file
to the AUTOEXEC.BAT file (here DEL is the DOS equivalent of rm), or
probably there's some way of hooking into the registry to achieve the
same effect.
The DeleteFileTransacted function marks a file for deletion on close.Coffee said:Thanks for any advice.
~ CP
want to delete an open file as this might have a big impact on the best
solution.
Mark McIntyre said:Coffee said:When doing so also say *why* you
want to delete an open file as this might have a big impact on the best
solution.
Thanks for the replies.
Basically I am trying to port the following UNIX code to Windows.
FILE *tmpfil;
char template[]="/tmp/XXXXXX";
mktemp(template);
tmpfil = fopen(template, "w+");
unlink(template);
This guarantees that the temporary file will be removed at the end of
execution even if the program terminates abnormally.
As has already been suggested, you need to ask in a windows
programming group. I strongly suspect windows has its own mechanism
for creating and clearing up temp files which will work very similarly
but using platform-specific functions.
BTW AFAIK the above code isn't guaranteed to work on Linux/Unix
either, if the app crashed between the fopen and the unlink.
jacob navia said:The DeleteFileTransacted function marks a file for deletion on
close. Therefore, the file deletion does not occur until the last
handle to the file is closed.
You should get the documentation, it is freely available by
Microsoft. Download the SDK.
Mark McIntyre said:Ben said:Mark McIntyre said:Coffee Pot wrote:
Basically I am trying to port the following UNIX code to Windows.
FILE *tmpfil;
char template[]="/tmp/XXXXXX";
mktemp(template);
tmpfil = fopen(template, "w+");
unlink(template);
This guarantees that the temporary file will be removed at the end of
execution even if the program terminates abnormally.
Note this sentence...
From his post above what he wanted was a guarantee the file would be
cleaned up. As the section you quoted made clear, for tmpfile() this
is implementation defined.
Agreed, but i suspect its at least as likely the file will get used
before being unlink()ed, so....
Mark McIntyre said:*shrug*. You may be right, but I dont propose to second-guess the
OP. He said he wanted a guaranteed method, C doesn't provide one, his
OS may, this isn't the place to ask about that.
Take a look in the %temp% directory just after crash-rebooting a
Windows PC by pulling the power cable.
??? You're having a laugh, surely.
Willem said:Doubtful. The usual trick is to unlink() it directly after
open()ing it.
(As you may or may not know, as long as the unlinked file
remains open, you can still use it.)
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.