views:

971

answers:

3

I need do delete a temporary file from my c++ windows application (developed in Borland C++ Builder). Currently I use a simple:

system("del tempfile.tmp");

This causes a console window to flash in front of my app and it doesn't look very professional. How do I do this without the console window?

+12  A: 

It sounds like you need the Win32 function DeleteFile(). You will need to #include <windows.h> to use it.

Paul Stephenson
Excelent, thanks! #include "windows.h"...
c0m4
I've edited to add your comment about including windows.h so it's clearer.
Paul Stephenson
Since the standard remove() function performs the same task and is a part of the standard, I would vote for it rather than the windows function.
Jon Trauntvein
Fair enough. I think it depends on the style of the rest of the code. If you're using Win32 functions in other places anyway then it's clearer to stick with DeleteFile(). Since the original question included very Windows-specific system calls I suggested DeleteFile() as a more Windows-y solution.
Paul Stephenson
+14  A: 

Or even the standard C library function int remove( const char *path );

Shane MacLaughlin
+2  A: 

For a slightly more portable (I.e. that works in both Windows and UNIX), I use unlink() or the ISO conformant _unlink() in io.h (unlink() for UNIX include unistd.h)
Remove() actually calls _unlink().

Roger Nelson