Solved

Overwrite a file

Posted on 2007-12-03
5
293 Views
Last Modified: 2013-12-03
Hi Experts,

I want to change the content of a file.I am using the WriteFile() function to overwrite the file, but this is very slow.So please, could you tell me another alternate ?

Thanks in advance.
0
Comment
Question by:tariq6000
5 Comments
 
LVL 19

Expert Comment

by:alb66
Comment Utility
You can use CFile class.
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 250 total points
Comment Utility
>>>> I am using the WriteFile() function to overwrite the file, but this is very slow.
I assume it is a text file. If writing a text file is slow, it is rarely the used write function which is responsible for it, but

- a running anti-virus scanner
- a slow or defragmented disk
- a disk which is nearly full
- some other process which was reading/writing from/to the disk
- some other process which consumes all CPU

Nevertheless, you can speedup writing the file by writing it with one single write. Assuming you have a CStringArray which contains all lines to write, you could concatenate all strings and have one write like that:

#include "stdafx.h"
#include <fstream>
using namespace std;
...

bool writeAllText(const CStringArray& strarr, const CString& strFilename)
{
      int size = strarr.GetSize();
      CString strall;
      for (int i = 0; i < size; ++i)
           strall += strarr[i] + "\r\n";
      ofstream ofs((const char*)strFilename), ios::out |ios::binary);
      if (!ofs)
      {
            AfxMessageBox(strFilename + " cannot be opened");
            return false;
      }
      if (!ofs.write((const char*)strall, strall.GetLength()))
      {
            int err = GetLastError();
            CString strerr;
            itoa(err, strerr.GetBuffer(12), 10);
            strerr.ReleaseBuffer(-1);
            AfxMessageBox(strFilename + " couldn't be written. Error = " + strerr);
            ofs.close();
            return false;
      }
      ofs.close();
      return true;
   
}


The above would write the text file in binary mode to spare some other checks. But as told, it hardly will be much faster if the *real* reason for slowness wasn't found.

Note, I assumed a non-UNICODE project. Tell me if I am wrong.

Regards, Alex
 
0
 
LVL 1

Assisted Solution

by:DunjinMaster
DunjinMaster earned 250 total points
Comment Utility
FWIW, I'm the author of a professional data-destruction toolkit I won't name so as to not violate any anti-unauthorized-advertising rules.

Usign WriteFile to overwrite a file is a perfectly valid exercise, but itsme is correct - for best speed you should blast data to the file in large segments, preferably aligned to cluster boundaries.

(You'll find that getting accurate cluster size information for all current 32/64-bit Windows releases will be far tougher than performing the actual overwrite!) If you're sending one byte at a time, don't - build at least 512-byte sequences (the normal sector size for a hard drive) and write those, and if you slightly exceed the file's size that's not generally a problem. (In fact if you're using cluster-sized data chunks you WANT to fill full clusters.)

Follow up the overwrite with a FlushFileBuffers call to force a cache writeout (VERY important! the I/O handler may choose to discard your changes for no apparent reason if you don't!), then close the handle and finally process the Windows message queue to give the disk acess caching system enough time to commit the file changes to disk.

DM
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

With most software applications trying to cater to multiple user needs nowadays, the focus is to make them as configurable as possible. For e.g., when creating Silverlight applications which will connect to WCF services, the service end point usuall…
After several hours of googling I could not gather any information on this topic. There are several ways of controlling the USB port connected to any storage device. The best example of that is by changing the registry value of "HKEY_LOCAL_MACHINE\S…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now