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
Solved

Overwrite a file

Posted on 2007-12-03
5
302 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
ID: 20401388
You can use CFile class.
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 250 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 250 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
java ^ examples 8 69
String manipulation in Visual Basic 7 71
x-bar in Google Sheets 2 69
What namespace do I need to import? 2 38
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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…

792 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