|
-
October 18th, 2010, 06:22 AM
#1
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.
-
October 18th, 2010, 06:55 AM
#2
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.
-
October 18th, 2010, 11:17 PM
#3
Re: Clear File Content
Thanks for your help.
-
October 19th, 2010, 02:21 AM
#4
Re: Clear File Content
 Originally Posted by monarch_dodra
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();
-
October 19th, 2010, 02:29 AM
#5
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.
-
October 19th, 2010, 03:11 AM
#6
Re: Clear File Content
 Originally Posted by monarch_dodra
What is stat?
stat
 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).
-
October 19th, 2010, 07:21 AM
#7
Re: Clear File Content
 Originally Posted by laserlight
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();
}
-
October 19th, 2010, 09:53 AM
#8
Re: Clear File Content
 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.
-
October 19th, 2010, 09:56 AM
#9
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.
-
October 19th, 2010, 06:14 PM
#10
Re: Clear File Content
 Originally Posted by monarch_dodra
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|