Solved

Help: Overwrite a few bytes in large file

Posted on 1997-07-17
4
256 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Mobile Keyboard covers the display of the TMemo 3 115
oracle global variables 4 80
CheckListBox usage 3 98
How to make Sign in, using Clientdataset? 1 43
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…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

751 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