CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Clear File Content

    Hello to all, may i know what is the approach to clear a file(delete the file content) ?

    Thanks.
    Thanks for your help.

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Clear File Content

    Opening a file for writing (without the append option) will clear the contents. Anything else would probably require external OS libraries.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  3. #3
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Clear File Content

    Thanks for your help.
    Thanks for your help.

  4. #4
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Clear File Content

    Quote Originally Posted by monarch_dodra View Post
    Opening a file for writing (without the append option) will clear the contents. Anything else would probably require external OS libraries.
    Opening a file for writing would truncate the file, the contents is still available on the disk though it needs special tools to recover it.

    To make sure that the contents never may be recovered you would need to overwrite the old contents, e.g. like that:

    Code:
        struct stat fs = { 0 };
        if (stat(filename.c_str(), &fs) == 0)
        {
             char buffer[4096] = { '\0' };
             std::ofstream ofs(filename.c_str(), std::ios::binary | std::ios::out);
             unsigned int sizf   = fs.st_size;
             unsigned int sizb  = (unsigned int)sizeof(buffer);
             unsigned int n;
             for (n = 0; n < sizf; n += sizb  )
             {
                   unsigned int nr = sizf - n;
                   if (nr > sizb)
                        nr = sizb;
                   ofs.write(buffer, nr);
             }
             ofs.close();

  5. #5
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Clear File Content

    1- What is stat?
    2- You are writing to your ostream, so the file is not empty at the end
    3- Whether or not the file contents on the disk are overwritten are 100% os defined. The OS may decide to do everything in memory, and to save the new file in another place, and keep the old file in some OS reserved space.

    If what you really want is to hard delete the file, you'll have to interface with the OS. C++ can't do it.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Clear File Content

    Quote Originally Posted by monarch_dodra
    What is stat?
    stat

    Quote Originally Posted by monarch_dodra
    2- You are writing to your ostream, so the file is not empty at the end
    It seems to me that itsmeandnobodyelse expected Peter_APIIT to combine his/her suggestion with your own. Looking at the simplicity of Peter_APIIT's original post, I think itsmeandnobodyelse's suggestion is overkill, but if secure file deletion was desired, then it may be insufficient since I have heard that certain bit patterns have to be used in order to be truly effective (besides your own concerns about the actual overwriting of contents on disk).
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Clear File Content

    Quote Originally Posted by laserlight View Post
    stat


    It seems to me that itsmeandnobodyelse expected Peter_APIIT to combine his/her suggestion with your own. Looking at the simplicity of Peter_APIIT's original post, I think itsmeandnobodyelse's suggestion is overkill, but if secure file deletion was desired, then it may be insufficient since I have heard that certain bit patterns have to be used in order to be truly effective (besides your own concerns about the actual overwriting of contents on disk).
    stat comes from sys/stat.h and is to retrieve the file size in the sample code. If you update an existing file in binary mode it should overwrite the existing disk space used by the previous contents. To make that sure you can open the file in read and write mode (ios::in | ios:ut | ios::binary). That way the file wasn't truncated at open and the zero buffer overwrites all existing contents.

    If you want to increase efficiency you may use a bigger buffer up to the file size of the file what should work on modern hardware if the files were not too huge and simplifies the code.

    Code:
    #include <sys/stat.h>
    #include <fstream>
    
    ...
        struct stat fs = { 0 };
        if (stat(filename.c_str(), &fs) == 0)
        {
              char * buffer = new char[fs.st_size]; 
              if (buffer != NULL)
              { 
                    memset(buffer, 0, fs.st_size);
                    std::ofstream ofs(filename.c_str(), std::ios::binary | std::ios::out | std::ios::in);
                    ofs.write(buffer, fs.st_size);
                    delete [] buffer;
                    ofs.close();
              }

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Clear File Content

    Quote Originally Posted by itsmeandnobodyelse
    If you want to increase efficiency you may use a bigger buffer up to the file size of the file what should work on modern hardware if the files were not too huge and simplifies the code.
    Of course, if the file is large enough, this might reduce efficiency instead. As for simplifying the code:
    Code:
    #include <sys/stat.h>
    #include <fstream>
    #include <vector>
    
    // ...
        struct stat fs = { 0 };
        if (stat(filename.c_str(), &fs) == 0)
        {
            std::vector<char> buffer(fs.st_size, 0);
            std::ofstream ofs(filename.c_str(), std::ios::binary | std::ios::out | std::ios::in);
            ofs.write(&buffer[0], buffer.size());
            ofs.close();
        }
    But again, it is my opinion that Peter_APIIT did not express any desire for this kind of file deletion, and if he did, merely overwriting once with 0s may be insufficient.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  9. #9
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Clear File Content

    I still say that doing this gives no guarantees that any actual hard disk space is overwritten. This is OS specific.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  10. #10
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Talking Re: Clear File Content

    Quote Originally Posted by monarch_dodra View Post
    I still say that doing this gives no guarantees that any actual hard disk space is overwritten. This is OS specific.
    Yes. And if we'd want to get totally pedantic: Taking into account SSDs, their wear leveling strategies and the ATA trim command, it can even be device specific...
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured