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

Points for WorkshopAlex

As promised...
  • 4
  • 2
1 Solution
Wim ten BrinkCommented:
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.) :-)
A1ternityAuthor Commented:
Lol thanks...
It's curious though since I've seen this done before "pts for Mr X"

Anyway...  Hope you get those!
Wim ten BrinkCommented:
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 http://www.experts-exchange.com/help.jsp#hi76 for more information about it.

This is why I've "enriched" this question by changing the original question. :-)
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Wim ten BrinkCommented:
Btw. You can accept the answer now. If someone disagrees, we will surely notice it soon enough. ;-)
A1ternityAuthor Commented:
Sorry thought I had already done it... there you go! :)
Wim ten BrinkCommented:
LOL. I thought so already, that's why I had to remind you. :-P

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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