Points for WorkshopAlex

Posted on 2004-11-18
Last Modified: 2010-04-05
As promised...
Question by:A1ternity
    LVL 17

    Accepted Solution

    Thanks, but technically you're not allowed to reward more than 500 points on a single question. Technically, you would have to ask a new question again...
    But okay, let's try to make it a neat question. I assume you could create a new question: "I need to insert the data at a position in the middle of another file without using a temporary file?"
    This would then be answered by me with:

    The code below lacks a few checks but that's just to make the principle of the technique a lot clearer...

    procedure InsertIntoFile( const Filename: string; const Data: string; Position: Integer );
      AFile: file;
      Buffer: array[ 0..4095 ] of char;
      EndOfFile: Int64;
      BufPos: Int64;
      DataLen: Int64;
      Size: Int64;
      AssignFile( AFile, Filename );
      Reset( AFile, 1 );
      EndOfFile := FileSize( AFile );
      Size := EndOfFile - Position;
      BufPos := EndOfFile - SizeOf( Buffer );
      DataLen := Length( Data );
      while ( Size > SizeOf( Buffer ) ) do begin
        Seek( AFile, BufPos );
        BlockRead( AFile, Buffer, SizeOf( Buffer ) );
        Seek( AFile, BufPos + DataLen );
        BlockWrite( AFile, Buffer, SizeOf( Buffer ) );
        BufPos := BufPos - SizeOf( Buffer );
        Size := Size - SizeOf( Buffer );
      if ( Size > 0 ) then begin
        Seek( AFile, Position );
        BlockRead( AFile, Buffer, Size );
        Seek( AFile, Position + DataLen );
        BlockWrite( AFile, Buffer, Size );
      Seek( AFile, Position );
      BlockWrite( AFile, PChar( Data )^, Length( Data ) );
      CloseFile( AFile );

    This method doesn't use any temporary file but uses some internal memory to store the data. Basically, I first move blocks of 4 KB within the file to some other position. Thus, because I use small blocks, I can even manipulate large files. Once I run out of blocks of 4 KB I move the remaining block after the position to it's new position. Once that is in place too, I seek the position where the data needs to be inserted and insert it there.
    One risk, though. Any exception in this routine will damage the original file...

    This solution is not simpler but you might consider it cleaner since you don't need temporary files.

    FYI, I used a string for the data that needs to be inserted. Of course, this could be anything that you want to insert, but you must have an easy way to determine the size of this data.
    Considering performance, I don't think it will be really faster than the accepted solution. It's just a different technique.

    Let's hope this satisfies the EE management. ;-) (Maybe they could rename this question for you.) :-)

    Author Comment

    Lol thanks...
    It's curious though since I've seen this done before "pts for Mr X"

    Anyway...  Hope you get those!
    LVL 17

    Expert Comment

    by:Wim ten Brink
    Yes, those posts have been put here before. And the EE team will keep them here if the question that they are related to was worth less than 500 points. But EE doesn't like it when Q's get more than 500 points so they are taking action against those questions.
    See for more information about it.

    This is why I've "enriched" this question by changing the original question. :-)
    LVL 17

    Expert Comment

    by:Wim ten Brink
    Btw. You can accept the answer now. If someone disagrees, we will surely notice it soon enough. ;-)

    Author Comment

    Sorry thought I had already done it... there you go! :)
    LVL 17

    Expert Comment

    by:Wim ten Brink
    LOL. I thought so already, that's why I had to remind you. :-P

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
    Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    760 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

    8 Experts available now in Live!

    Get 1:1 Help Now