Solved

Dynamic arrays and Blockread

Posted on 2000-03-23
7
989 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 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