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!
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(Sour ceStream, Length);
SourceStream.Free;
TargetStream.Free;
end;
Ciao, Mike
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,
TargetStream := TFileStream.Create(Target,
SourceStream.Position := Start;
TargetStream.CopyFrom(Sour
SourceStream.Free;
TargetStream.Free;
end;
Ciao, Mike
ASKER