Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Dynamic arrays and Blockread

Posted on 2000-03-23
7
Medium Priority
?
1,011 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 400 total points
ID: 2650621
This is it!

BlockRead(fp,temp[0],14);
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

670 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