• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 143
  • Last Modified:

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!
  • 2
1 Solution
1) Use a file stream and open the file (not necessarily an executable).
2) Set the streams position to the one you need.
3) Create a target sream for the part to extract.
4) Call TargetStream.CopyFrom(SourceStream, NumberOfBytesToCopy).
5) Close both streams.

Ciao, Mike
yingkitAuthor Commented:
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;

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

Ciao, Mike
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now