How to delete a file from C++ program

L

Leslaw Bieniasz

Hi,

I am using a temporary binary file which I open and close
by
fstream::eek:pen()
and
fstream::close()
After using the temporary, I would like to delete it from the
disk. Is there any way of doing this from the level of the
C++ program which operates in the console mode under MS Windows?
I mean I don't want to quit the program and use the system commands
manually, and I don't want to use GUI for deleting the file.

L.B.


!!! PLEASE NOTE MY NEW ADDRESS SINCE January 1st, 2008, INDICATED BELOW !!!
*-------------------------------------------------------------------*
| Dr. Leslaw Bieniasz, |
| Institute of Physical Chemistry of the Polish Academy of Sciences,|
| Department of Complex Systems and Chemical |
| Processing of Information |
| ul. Niezapominajek 8, 30-239 Cracow, Poland. |
| tel. (room) +48 (12) 6395212 |
| tel./fax. (secretariat) +48 (12) 4251923 |
| E-mail: (e-mail address removed) |
*-------------------------------------------------------------------*
| Interested in Computational Electrochemistry? |
| Visit my web site: http://www.cyf-kr.edu.pl/~nbbienia |
*-------------------------------------------------------------------*
 
M

Matthias Buelow

Leslaw said:
I am using a temporary binary file which I open and close
by
fstream::eek:pen()
and
fstream::close()
After using the temporary, I would like to delete it from the
disk. Is there any way of doing this from the level of the
C++ program which operates in the console mode under MS Windows?
I mean I don't want to quit the program and use the system commands
manually, and I don't want to use GUI for deleting the file.

remove()

There's also tmpfile() for temporary functions, which will take care of
removing the file itself.
 
A

Alberto Bignotti

try:

#include <stdio.h>
#include <string>

bool file_delete(std::string filename)
{
if( _unlink( filename.c_str() ) != 0)
return false;

return true;
}
 
M

Matthias Buelow

Alberto said:
try:

#include <stdio.h>
#include <string>

bool file_delete(std::string filename)
{
if( _unlink( filename.c_str() ) != 0)
return false;

return true;
}

This isn't portable.
 
M

Michael DOUBEZ

Leslaw Bieniasz a écrit :
Hi,

I am using a temporary binary file which I open and close
by
fstream::eek:pen()
and
fstream::close()
After using the temporary, I would like to delete it from the
disk. Is there any way of doing this from the level of the
C++ program which operates in the console mode under MS Windows?
I mean I don't want to quit the program and use the system commands
manually, and I don't want to use GUI for deleting the file.

You can use "int remove(const char* filename);" from cstdio.
http://www.cplusplus.com/reference/clibrary/cstdio/remove.html

Michael
!!! PLEASE NOTE MY NEW ADDRESS SINCE January 1st, 2008, INDICATED BELOW !!!
Long signature
Please disable your signature on usenet.
 
O

online_wan

Hi,

I am using a temporary binary file which I open and close
by
fstream::eek:pen()
and
fstream::close()
After using the temporary, I would like to delete it from the
disk. Is there any way of doing this from the level of the
C++ program which operates in the console mode under MS Windows?
I mean I don't want to quit the program and use the system commands
manually, and I don't want to use GUI for deleting the file.

L.B.

!!! PLEASE NOTE MY NEW ADDRESS SINCE January 1st, 2008, INDICATED BELOW !!!
*-------------------------------------------------------------------*
| Dr. Leslaw Bieniasz, |
| Institute of Physical Chemistry of the Polish Academy of Sciences,|
| Department of Complex Systems and Chemical |
| Processing of Information |
| ul. Niezapominajek 8, 30-239 Cracow, Poland. |
| tel. (room) +48 (12) 6395212 |
| tel./fax. (secretariat) +48 (12) 4251923 |
| E-mail: (e-mail address removed) |
*-------------------------------------------------------------------*
| Interested in Computational Electrochemistry? |
| Visit my web site:http://www.cyf-kr.edu.pl/~nbbienia |
*-------------------------------------------------------------------*


/* UNLINK.C: This program uses _unlink to delete UNLINK.OBJ. */

#include <stdio.h>

void main( void )
{
if( _unlink( "unlink.obj" ) == -1 )
perror( "Could not delete 'UNLINK.OBJ'" );
else
printf( "Deleted 'UNLINK.OBJ'\n" );
}

=======================
from msdn
 
M

Michael DOUBEZ

Fei Rao a ¨¦crit :
Hello Leslaw,

The deletion of a file turns out to use system call such as unlink() and
remove() (on UNIX / Linux). I think it has nothing to do with the
fstream, although there may be some wrappers in C++ libraries.

Note that remove() is standard while unlink() is only POSIX.

Please, don't top-post

Michael
 
F

Fei Rao

Hello Leslaw,

The deletion of a file turns out to use system call such as unlink() and
remove() (on UNIX / Linux). I think it has nothing to do with the fstream,
although there may be some wrappers in C++ libraries.
 
L

Leslaw Bieniasz

/* UNLINK.C: This program uses _unlink to delete UNLINK.OBJ. */

#include <stdio.h>

void main( void )
{
if( _unlink( "unlink.obj" ) == -1 )
perror( "Could not delete 'UNLINK.OBJ'" );
else
printf( "Deleted 'UNLINK.OBJ'\n" );
}

=======================
from msdn


OK Guys, all your suggestions require mixing of C++ streams
with some stdio.h functions or the like, and such mixing
(if I understand) is generally not recommended. My intention
is to find a solution which involves C++ streams only.
Is this possible?

L.B.



!!! PLEASE NOTE MY NEW ADDRESS SINCE January 1st, 2008, INDICATED BELOW !!!
*-------------------------------------------------------------------*
| Dr. Leslaw Bieniasz, |
| Institute of Physical Chemistry of the Polish Academy of Sciences,|
| Department of Complex Systems and Chemical |
| Processing of Information |
| ul. Niezapominajek 8, 30-239 Cracow, Poland. |
| tel. (room) +48 (12) 6395212 |
| tel./fax. (secretariat) +48 (12) 4251923 |
| E-mail: (e-mail address removed) |
*-------------------------------------------------------------------*
| Interested in Computational Electrochemistry? |
| Visit my web site: http://www.cyf-kr.edu.pl/~nbbienia |
*-------------------------------------------------------------------*
 
I

Ian Collins

Leslaw said:
OK Guys, all your suggestions require mixing of C++ streams
with some stdio.h functions or the like, and such mixing
(if I understand) is generally not recommended. My intention
is to find a solution which involves C++ streams only.
Is this possible?
A file is a file, which can be removed. A stream is a means or reading
a files.

What's your problem with using a standard library function for its
intended use?
!!! PLEASE NOTE MY NEW ADDRESS SINCE January 1st, 2008, INDICATED BELOW !!!

Please do something (like remove) your overly long signature.
 
M

Michael DOUBEZ

Leslaw Bieniasz a ¨¦crit :
OK Guys, all your suggestions require mixing of C++ streams
with some stdio.h functions or the like, and such mixing
(if I understand) is generally not recommended. My intention
is to find a solution which involves C++ streams only.
Is this possible?
[snips]

It is not. streams do not care about files, they are a concept.
The fstream lib is only a helper to connect a file sink to a stream.

There is no problem in mixing <fstream> and <cstdio>. They are both part
of the c++ standard so remove() should be fairly portable.

Michael
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top