Save Array of Byte to File and Load it later

Victor Guilherme
Victor Guilherme used Ask the Experts™
on
Hi, I've been banging my head with trying to come up with a way on Pascal on how to create a function that saves an array of byte to file while keeping its integrity from the fist to the last byte.

I came up with two ideas, either I save the data to the file in its binary form or I convert the binary to hex then save.
When I need to load, if its hex I can decode and convert it to an array of bytes again.

I'm really stuck, because I looked over alot on the past days and didnt find anything remotely close to it. I've began recently working with Pascal so I'm not that experienced with it.
Going C++ would solve me issue but that is not an option here, I need to do it on Pascal.

So if anyone could come up with a way for me to save the array of bytes then load it again later on I would be really thankful.

I could also use a function to encode/decode the bytes, but i have no clue on how would that work.

This is the array I need to save

hmm: array [0..18954] of BYTE;

I have pretty much no base code cause everything I came up with brings error on the IDE, sorry.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software architect
Top Expert 2012
Commented:
There are few ways - one is using FileStream object.
procedure WriteDataArray(FileName: String; var Arr; sz: Integer);
var
  fs: TFileStream;
begin
  fs:= TFileStream.Create(FileName, fmCreate);
  try
    fs.WriteBuffer(Arr, sz);
  finally
    fs.Free;
  end;
end;

procedure ReadDataArray(FileName: String; var Arr; sz: Integer);
var
  fs: TFileStream;
begin
  fs:= TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
  try
    fs.ReadBuffer(Buf, sz);
  finally
    fs.Free;
  end;
end;

//call ...
WriteDataArray('c: ... test.dat', hmm, SizeOf(hmm));
ReadDataArray('c: ... test.dat', hmm, SizeOf(hmm));

Open in new window

Sinisa VukSoftware architect
Top Expert 2012

Commented:
Hi!
Did you manage your code to work? Or you have some (other) issues?

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial