Hello to all, may i know what is the approach to clear a file(delete the file content) ?
Thanks.
Printable View
Hello to all, may i know what is the approach to clear a file(delete the file content) ?
Thanks.
Opening a file for writing (without the append option) will clear the contents. Anything else would probably require external OS libraries.
Thanks for your help.
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();
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.
statQuote:
Originally Posted by monarch_dodra
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).Quote:
Originally Posted by monarch_dodra
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::out | 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();
}
Of course, if the file is large enough, this might reduce efficiency instead. As for simplifying the code:Quote:
Originally Posted by itsmeandnobodyelse
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.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();
}
I still say that doing this gives no guarantees that any actual hard disk space is overwritten. This is OS specific.