Solved

Help: Overwrite a few bytes in large file

Posted on 1997-07-17
4
233 Views
Last Modified: 2010-04-06
Hello, there!

I am writing a database programme, it is require to handle a very large database file (about 10 MB).
It is quite hard for me to change only a few bytes in file. It is because the method I use is linear loading.
For Example, my db file have 6 element ABCDEF,  If I want to overwrite E by Z (ABCDZF),
I need to
1, Read ABCD
2, Write ABCD
3, Write Z
4, skip reading E
5, Read F
6, Write F
7, Close the File
It is no problem, if the Database is so small, but my is too large for that operation, If everytime I just change 3 bytes in 10MB file at 9MB after the head of file. It will be too time consuming.
Is it possible to skip operation [1,2,5,6], but still can change E to Z?
Thanks for reading.

James.
0
Comment
Question by:jhui
  • 2
4 Comments
 
LVL 4

Expert Comment

by:erajoj
ID: 1339074
Maybe I misunderstand you but...
...have you looked at "blockread" & "blockwrite" & "seek" ?
If you know the position of the "E" then just do:
...
  Seek(FileHandle, E_Pos);
  BlockWrite(FileHandle, "Z", 1);
...

/// John

0
 

Author Comment

by:jhui
ID: 1339075
Well, I am sorry to reject your answer, actually your answer is a quite good hits to solve the problem, but I do need some example of it because the Pascal / Delphi online Help do not give actual explaination of it.
If you can show me the coding, only a very simple one will do. Thanks, anyway.

Here is some more information I hope to clearify what I ask:
The file I creat myself is a text file, and it is quite a huge in size (about 10 MB).
As I only know how to create an sequence file, so If I want to change only a few bytes(say 10000byte after the begining of the file) in the file, I need to read all the data 10000 and write to a temporary file, and the write the code I want to change, then write the data after it to the temp. file. At last delete the replace the temp. file to data file.
I do think it is too complex for only to change a few bytes in the file. I think it is possible to skip these dump readings and writings, because it HEX-Edit or PC-Tools or DiskEdit(Norton Utility) can edit a few data in a file and do not need to do lot of copying operation, right.
I hope it is more clear than what I have said before.

Thanks.

James
0
 
LVL 3

Expert Comment

by:mirek071497
ID: 1339076
"erajoj" can write code for you, but why can't you use Delphi database. it is very easy.  Don't work at your own file format !!!
But when You need work on own format you must more learn about database structures and make keys.
When You need find any record now you must search by whole file !
If you need more help about databases you can mail to me, but remember that my english is very poor.

mirek@izabell.itcomp.tpnet.pl
0
 
LVL 4

Accepted Solution

by:
erajoj earned 50 total points
ID: 1339077
These functions will do the trick(?):

function ReplaceStringAtPos(FileName, Txt: string; Position: Integer): Boolean;
var
  F: file;
begin
  try
    AssignFile(F, FileName);
    Reset(F, 1);
    Seek(F, Position);
    BlockWrite(F, PChar(Txt), Length(Txt));
    CloseFile(F);
    Result := True;
  except
    Result := False;
    // catch error here
  end;
end;

function GetStringAtPos(FileName: string; Position, Length: Integer): string;
var
  F: file;
begin
  try
    AssignFile(F, FileName);
    FileMode := 0; // Read only
    Reset(F, 1);
    Seek(F, Position);
    SetLength(Result, Length);
    BlockRead(F, PChar(Result), Length);
    CloseFile(F);
  except
    Result := '';
    // catch error here
  end;
end;

This is not tested, but it should work.
BTW: I agree with mirek, there are MANY advantages with
"pret-â-porter"-databases.

/// John

0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 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

13 Experts available now in Live!

Get 1:1 Help Now