Solved

Help: Overwrite a few bytes in large file

Posted on 1997-07-17
4
245 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error E2158 compiling with Delphi XE10 Seattle 2 100
update joined tables 2 46
Installshield for Embarcadero EX 10.1 Berlin 4 40
Delphi: barcode reading on android platform 1 30
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

770 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