Link to home
Start Free TrialLog in
Avatar of yingkit
yingkit

asked on

Access a binary file.

Suppose a binary file, say SAMPLE.EXE is 111,234 bytes long.
If I want to "extract" some contents from sample.exe and output it to another file(extract.dat), for instance, copy from 10,000th bytes to 12,499th bytes, a total of 2,500 bytes.
How can I do so?  Thanks in advance!
ASKER CERTIFIED SOLUTION
Avatar of Lischke
Lischke

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of yingkit
yingkit

ASKER

Can you pls provide me detail source codes?  I am only a beginner... :-(
Right, here's the code:

procedure ExtractFile(const Source, Target: String; Start, Length: Cardinal);

// Extracts from a file which name is passed in Target from
// another file which name is passed in Source. Both strings
// must include valid pathes. Start determines the start
// position in Source and Length the the length of the file
// to be extracted.

var SourceStream, TargetStream: TFileStream;

begin
  SourceStream := TFileStream.Create(Source, fmOpenRead);
  TargetStream := TFileStream.Create(Target, fmOpenWrite);
  SourceStream.Position := Start;
  TargetStream.CopyFrom(SourceStream, Length);
  SourceStream.Free;
  TargetStream.Free;
end;

Ciao, Mike