?
Solved

Overwrite a file

Posted on 2007-12-03
5
Medium Priority
?
306 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 19

Expert Comment

by:alb66
ID: 20401388
You can use CFile class.
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 1000 total points
ID: 20402016
>>>> 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 1000 total points
ID: 21388198
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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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…

770 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