Points for WorkshopAlex

As promised...
A1ternityAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Wim ten BrinkSelf-employed developerCommented:
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 );
var
  AFile: file;
  Buffer: array[ 0..4095 ] of char;
  EndOfFile: Int64;
  BufPos: Int64;
  DataLen: Int64;
  Size: Int64;
begin
  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 );
  end;
  if ( Size > 0 ) then begin
    Seek( AFile, Position );
    BlockRead( AFile, Buffer, Size );
    Seek( AFile, Position + DataLen );
    BlockWrite( AFile, Buffer, Size );
  end;
  Seek( AFile, Position );
  BlockWrite( AFile, PChar( Data )^, Length( Data ) );
  CloseFile( AFile );
end;

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.) :-)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
A1ternityAuthor Commented:
Lol thanks...
It's curious though since I've seen this done before "pts for Mr X"

Anyway...  Hope you get those!
0
Wim ten BrinkSelf-employed developerCommented:
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. :-)
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Wim ten BrinkSelf-employed developerCommented:
Btw. You can accept the answer now. If someone disagrees, we will surely notice it soon enough. ;-)
0
A1ternityAuthor Commented:
Sorry thought I had already done it... there you go! :)
0
Wim ten BrinkSelf-employed developerCommented:
LOL. I thought so already, that's why I had to remind you. :-P
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.