How to delete a file from C++ program

Discussion in 'C++' started by Leslaw Bieniasz, Feb 27, 2008.

  1. 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: |
    *-------------------------------------------------------------------*
    | Interested in Computational Electrochemistry? |
    | Visit my web site: http://www.cyf-kr.edu.pl/~nbbienia |
    *-------------------------------------------------------------------*
     
    Leslaw Bieniasz, Feb 27, 2008
    #1
    1. Advertising

  2. Leslaw Bieniasz wrote:

    > 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.
     
    Matthias Buelow, Feb 27, 2008
    #2
    1. Advertising

  3. try:

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

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

    return true;
    }


    "Leslaw Bieniasz" <> ha scritto nel messaggio
    news:p-kr.edu.pl...
    >
    >
    > 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: |
    > *-------------------------------------------------------------------*
    > | Interested in Computational Electrochemistry? |
    > | Visit my web site: http://www.cyf-kr.edu.pl/~nbbienia |
    > *-------------------------------------------------------------------*
     
    Alberto Bignotti, Feb 27, 2008
    #3
  4. Alberto Bignotti wrote:
    > 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.
     
    Matthias Buelow, Feb 27, 2008
    #4
  5. 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.
     
    Michael DOUBEZ, Feb 27, 2008
    #5
  6. Leslaw Bieniasz

    online_wan Guest

    On 2ÔÂ27ÈÕ, ÏÂÎç7ʱ50·Ö, Leslaw Bieniasz <> wrote:
    > 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: |
    > *-------------------------------------------------------------------*
    > | 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
     
    online_wan, Feb 27, 2008
    #6
  7. 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.
    >
    >> 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.

    >
    >


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

    Please, don't top-post

    Michael
     
    Michael DOUBEZ, Feb 27, 2008
    #7
  8. Leslaw Bieniasz

    Fei Rao Guest

    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.

    > 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.
     
    Fei Rao, Feb 28, 2008
    #8
  9. On Wed, 27 Feb 2008, online_wan wrote:

    > On 2ÔÂ27ÈÕ, ÏÂÎç7ʱ50·Ö, Leslaw Bieniasz <> wrote:
    > > 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: |
    > > *-------------------------------------------------------------------*
    > > | 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



    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: |
    *-------------------------------------------------------------------*
    | Interested in Computational Electrochemistry? |
    | Visit my web site: http://www.cyf-kr.edu.pl/~nbbienia |
    *-------------------------------------------------------------------*
     
    Leslaw Bieniasz, Feb 28, 2008
    #9
  10. Leslaw Bieniasz

    Ian Collins Guest

    Leslaw Bieniasz wrote:
    >
    > 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.

    --
    Ian Collins.
     
    Ian Collins, Feb 28, 2008
    #10
  11. 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
     
    Michael DOUBEZ, Feb 28, 2008
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sandeep Grover

    delete on delete !

    Sandeep Grover, Jul 12, 2003, in forum: C++
    Replies:
    19
    Views:
    640
    Chris \( Val \)
    Jul 22, 2003
  2. HeroOfSpielburg
    Replies:
    1
    Views:
    402
    Alf P. Steinbach
    Aug 6, 2003
  3. 0to60
    Replies:
    4
    Views:
    320
    Jerry Coffin
    Dec 19, 2003
  4. Mathieu Malaterre

    delete NULL, what about delete[] NULL

    Mathieu Malaterre, Aug 17, 2004, in forum: C++
    Replies:
    2
    Views:
    3,852
    Mathieu Malaterre
    Aug 17, 2004
  5. Replies:
    3
    Views:
    8,467
Loading...

Share This Page