• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 310
  • Last Modified:

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.
0
tariq6000
Asked:
tariq6000
2 Solutions
 
alb66Commented:
You can use CFile class.
0
 
itsmeandnobodyelseCommented:
>>>> 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
 
DunjinMasterCommented:
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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Tackle projects and never again get stuck behind a technical roadblock.
Join Now