Solved

Dynamic arrays and Blockread

Posted on 2000-03-23
7
971 Views
Last Modified: 2008-02-26
I'm trying to read a section of a file into a dynamically allocated array of byte.

My code looks like this

var
   temp : array of byte;
    fp : file of byte;

begin
   Assignfile(fp,"file.bin");
   Reset(fp);
   Setlength(temp,14);
   Blockread(fp,temp,14)
end;

The code compiles, and blockread thinks it's written 14 bytes, but when I look at temp[n] there's nothing there. When I replace temp with a fixed size array ( as in temp : array[0..13] of byte) everything works fine...

What's going on? And more importantly, how do I fix it?

cheers,

Adam...
0
Comment
Question by:AJFleming
7 Comments
 
LVL 13

Expert Comment

by:Epsylon
ID: 2650596
Try FileRead instead of BlockRead...
0
 
LVL 6

Expert Comment

by:edey
ID: 2650616
or PByteArray instead or array of byte?

GL
Mike
0
 
LVL 13

Accepted Solution

by:
Epsylon earned 100 total points
ID: 2650621
This is it!

BlockRead(fp,temp[0],14);
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 2

Expert Comment

by:PeterLarsen
ID: 2650657
I agree with Epsylon.
Epsylon points to the first char (byte) in the stream. PByteArray would do the same.
0
 
LVL 1

Author Comment

by:AJFleming
ID: 2652658
OK, well it works... Now, for an A grade, (which I've already given,) care to tell me why?

My assumption is that dynamic arrays in Delphi work differently to arrays in C... Thus, whereas

 fred[0] == fred in C
 fred[0] <> fred in Delphi...

Correct? If so, can anyone give me a convincing explaination at to why?

cheers for the quick response,

Adam...
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2652794
The different between the declarations

   temp1: array of byte;
   temp2: array[0..13] of byte;

is that temp1 is a pointer to a buffer (after calling SetLength of course) and temp2 is an array on its own.

Note this too:

sizeof(temp1) returns 4 no matter what size the dynamic array has
sizeof(temp2) returns 14


To pass these arrays as buffer you can add [0] after the name.

Blockread(fp,temp1,14) passed the pointer to the function so it will fail
Blockread(fp,temp1[0],14) passed the address of the first byte of the array
Blockread(fp,temp2,14) passes the address of the array
Blockread(fp,temp2[0],14) passes the address of the first byte of the array which is the same as the address of the array
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2652807
Small correction:

Blockread(fp,temp1,14) passed the address of the pointer so it will fail
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now