Solved

Help: Overwrite a few bytes in large file

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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 this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Suggested Courses
Course of the Month4 days, 5 hours left to enroll

630 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