Rewrite file with 0’s. What am I doing wrong? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Rewrite file with 0’s. What am I doing wrong? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I want rewrite file with 0’s. It only write a few bytes.

My code:

int fileSize = boost::filesystem::file_size(filePath);

int zeros[fileSize] = { 0 };

boost::filesystem::path rewriteFilePath{filePath};
boost::filesystem::ofstream rewriteFile{rewriteFilePath, std::ios::trunc};
rewriteFile << zeros;

Also… Is this enough to shred the file? What should I do next to make the file unrecoverable?

EDIT: Ok. I rewrited my code to this. Is this code ok to do this?

int fileSize = boost::filesystem::file_size(filePath);

boost::filesystem::path rewriteFilePath{filePath};
boost::filesystem::ofstream rewriteFile{rewriteFilePath, std::ios::trunc};

for(int i = 0; i < fileSize; i++) {
    rewriteFile << 0;
}

Answer

There are several problems with your code.

  1. int zeros[fileSize] = { 0 };

    You are creating an array that is sizeof(int) * fileSize bytes in size. For what you are attempting, you need an array that is fileSize bytes in size instead. So you need to use a 1-byte data type, like (unsigned) char or uint8_t.

    But, more importantly, since the value of fileSize is not known until runtime, this type of array is known as a “Variable Length Array” (VLA), which is a non-standard feature in C++. Use std::vector instead if you need a dynamically allocated array.

  2. boost::filesystem::ofstream rewriteFile{rewriteFilePath, std::ios::trunc};

    The trunc flag truncates the size of an existing file to 0. What that entails is to update the file’s metadata to reset its tracked byte size, and to mark all of the file’s used disk sectors as available for reuse. The actual file bytes stored in those sectors are not wiped out until overwritten as sectors get reused over time. But any bytes you subsequently write to the truncated file are not guaranteed to (and likely will not) overwrite the old bytes on disk. So, do not truncate the file at all.

  3. rewriteFile << zeros;

    ofstream does not have an operator<< that takes an int[], or even an int*, as input. But it does have an operator<< that takes a void* as input (to output the value of the memory address being pointed at). An array decays into a pointer to the first element, and void* accepts any pointer. This is why only a few bytes are being written. You need to use ofstream::write() instead to write the array to file, and be sure to open the file with the binary flag.

Try this instead:

int fileSize = boost::filesystem::file_size(filePath);

std::vector<char> zeros(fileSize, 0);

boost::filesystem::path rewriteFilePath(filePath);
boost::filesystem::ofstream rewriteFile(rewriteFilePath, std::ios::binary);
rewriteFile.write(zeros.data()/*&zeros[0]*/, fileSize);

That being said, you don’t need a dynamically allocated array at all, let alone one that is allocated to the full size of the file. That is just a waste of heap memory, especially for large files. You can do this instead:

int fileSize = boost::filesystem::file_size(filePath);

const char zeros[1024] = {0}; // adjust size as desired...

boost::filesystem::path rewriteFilePath(filePath);
boost::filesystem::ofstream rewriteFile(rewriteFilePath, std::ios::binary);

int loops = fileSize / sizeof(zeros);
for(int i = 0; i < loops; ++i) {
    rewriteFile.write(zeros, sizeof(zeros));
}
rewriteFile.write(zeros, fileSize % sizeof(zeros));

Alternatively, if you open a memory-mapped view of the file (MapViewOfFile() on Windows, mmap() on Linux, etc) then you can simply use std::copy() or std::memset() to zero out the bytes of the entire file directly on disk without using an array at all.

Also… Is this enough to shred the file?

Not really, no. At the physical hardware layer, overwriting the file just one time with zeros can still leave behind remnant signals in the disk sectors, which can be recovered with sufficient tools. You should overwrite the file multiple times, with varying types of random data, not just zeros. That will more thoroughly scramble the signals in the sectors.

We are here to answer your question about Rewrite file with 0’s. What am I doing wrong? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji