Overwrite a file

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.
Who is Participating?
itsmeandnobodyelseConnect With a Mentor Commented:
>>>> 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);
            AfxMessageBox(strFilename + " couldn't be written. Error = " + strerr);
            return false;
      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
You can use CFile class.
DunjinMasterConnect With a Mentor Commented:
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.