Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

read a whole file into a dynamic byte array.

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
How do i in very simple code open, read, and close the whole of a binary file into a dynamic byte array.
is there a simple command to load the whole data and resize the array automaticcal or do i have to determine the size of the file and readjust the array to match it before loading the data in.?
 
The problem was you using BlockRead with a dynamic array (which is indexed from 0 as opposed to 1).

Code:
        afile: file of byte;
        buffer: array of byte;
        i: Integer;

        AssignFile(afile, 'filename');
        Reset(afile);
        SetLength(buffer, FileSize(afile));
        For i := 1 to FileSize(afile) do
                Read(afile, buffer[i - 1]);
        CloseFile(afile);
 
Thanks for the help mike,

But wouldn't the loop be a bit slow on a huge file?
I was hoping to 'block' load the data in.
reading in a byte at a time is going to have a huge speed implication isnt it?

I'm coming from a VB background so maybe im wrong!

Please help!

 
You could use a TMemoryStream object. Its LoadFromFile method automatically allocates memory and you don't need to bother opening/closing files.
 
Once again many thanks...

could i be really cheeky and ask for an example!!!!
 
Well basically it's just:

Code:
tempStream: TMemoryStream;

tempStream := TMemoryStream.Create;
tempStream.LoadFromFile('filename');

...

tempStream.Free;

Whether or not using this method is suitable for you depends on what you want to do with the data once you've loaded it in.
 
Im writing a piece of code that 'joins' 2 files, of random length, in basic i just redim an array of bytes and read the array in.

(i may need to modify the data so it has to be into an array or editable memory area)

everytime i try it in delphi with any method it crashes big time.

if i use static arrays it works fine. but as soon as i change the code to use a dynamic array it crashes with error 87 or something. its quite annoying.

Im on the verge of giving up.
 
I too have just spent some frustrating time trying to read a file into a dynamic array (when it worked perfectly with a static array). It's now solved.

I'm using the Read function of TFileStream.
The problem (and thence the solution) are as follows (note that this is pseudo-code with the two types of storage mixed for brevity):

var
fi : TFileStream;
dataStat : array [1..1000] of char;
dataDyn : array of char;
begin
....
SetLength(dataDyn, 1000);
fi.read(dataStat, 1000); // works fine
fi.read(dataStat[0], 1000) // also works fine
fi.read(dataDyn, 1000); // compiles OK but crashes at run time
fi.read(dataDyn[0], 1000); // works OK

So - with static arrays, it seems you can use either data or data[0] forms.
With dynamic arrays it seems you have to use the data[0] form. Doesn't seem logical but ...

I hope that's of some use.

Eddie
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top