Solved

Help: Overwrite a few bytes in large file

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

Question has a verified solution.

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

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…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

830 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