Solved

Dynamic arrays and Blockread

Posted on 2000-03-23
7
1,005 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 100 total points
ID: 2650621
This is it!

BlockRead(fp,temp[0],14);
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month8 days, 3 hours left to enroll

617 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