Board index » delphi » Writing pointer records to file

Writing pointer records to file

Hi,

I am having some problems getting a pointer record to add itself to a file.
All the write statements are Type Mismatches.......

==Declaration Stuff
type
rpntr = ^recdata;
    recdata = record
    Name: string;
        kName: array[1..30] of string;
   kScore: array[1..30] of integer;
   Numrec: integer;
end;

Var
    fdat: file of rpntr; <===  I suspect the problem lies here
    db: array[1..5] of rpntr;

==The Code in the procedure==

assign(fdat, 'data.tez');
  rewrite(fdat);

== Method 1 ==

  write(fdat, db[dbi]);

== Method 2  ==

  with db[dbi]^ do
  begin
    writeln(Name);
  end;
  close(fdat);

Also is it better to make the object the pointer and the fields normal (like
i have done) or the object normal and the fields pointers, or everything
pointers?

BTW It was only today i started trying to learn about pointers, and only a
few hours ago i tried to get inputs/ouputs to a file.

Thankyou

Regards,
Terence Siganakis

 

Re:Writing pointer records to file


It must be file of recdata, or else you''l try to put the pointer in the
memory. Then it must become write(fdat,db[dbi]^) too
Terence Siganakis heeft geschreven in bericht
<7cfqma$kp...@m2.c2.telstra-mm.net.au>...
Quote
>Hi,

>I am having some problems getting a pointer record to add itself to a file.
>All the write statements are Type Mismatches.......

>==Declaration Stuff
>type
>rpntr = ^recdata;
>    recdata = record
>    Name: string;
>        kName: array[1..30] of string;
>   kScore: array[1..30] of integer;
>   Numrec: integer;
>end;

>Var
>    fdat: file of rpntr; <===  I suspect the problem lies here
>    db: array[1..5] of rpntr;

>==The Code in the procedure==

>assign(fdat, 'data.tez');
>  rewrite(fdat);

>== Method 1 ==

>  write(fdat, db[dbi]);

>== Method 2  ==

>  with db[dbi]^ do
>  begin
>    writeln(Name);
>  end;
>  close(fdat);

>Also is it better to make the object the pointer and the fields normal
(like
>i have done) or the object normal and the fields pointers, or everything
>pointers?

>BTW It was only today i started trying to learn about pointers, and only a
>few hours ago i tried to get inputs/ouputs to a file.

>Thankyou

>Regards,
>Terence Siganakis

Re:Writing pointer records to file


Quote
Terence Siganakis wrote:

> I am having some problems getting a pointer record to add itself to a file.
> All the write statements are Type Mismatches.......

> ==Declaration Stuff
> type
> rpntr = ^recdata;
>     recdata = record
>     Name: string;
>         kName: array[1..30] of string;
>    kScore: array[1..30] of integer;
>    Numrec: integer;
> end;

> Var
>     fdat: file of rpntr; <===  I suspect the problem lies here
>     db: array[1..5] of rpntr;

Obviously you have serious troubles with the basics about pointers.

Instead of giving an answer to the particular question: please read
following articles:
http://www.geocities.com/SiliconValley/2926/tpf.html
page FAQ, articles "Turbo Pascal memory considerations, POINTER PRIMER"
and "Dynamic arrays".
--
Franz Glaser, Glasau 3, A-4191 Vorderweissenbach Austria ++43-7219-7035
Muehlviertler Elektronik Glaser. Industrial control and instrumentation
-----------------------------------------------------------------------
http://members.eunet.at/meg-glaser           mailto:meg-gla...@eunet.at
http://www.geocities.com/~franzglaser  http://members.xoom.com/f_glaser

Re:Writing pointer records to file


Quote
Terence Siganakis wrote:

> I have read both those articles, but i still dont understand what in the
> code made you think that i have serious troubles with the basics of pointers
> (which is completely true).  Is it my use of arrays (should i be using
> linked lists or something like that)?

     Pointers provide you with a means to link together, IN MEMORY,
records of some base type.  For comparison, consider an array.  Here,
the "records" are strung one after the other in memory, and you only
need an "index" (the array index) to get to the record you want.

     With pointers, the means of tying records together becomes much
more flexible (and error-prone?), and dependent on the programmer.  The
actual location of the records in memory becomes arbitrary, since the
pointers "point to" them.

     The key idea here is that the pointer structure exists almost
independently of the rest of the data in the record, and only describes
the structure in memory.  Thus if you are going to represent the data on
some other medium (for example, on an external file, or even the same
structure on another machine), the actual values of the pointer
variables are NOT fixed, but highly machine and run-dependent.

     Suppose, for example, you are building a simple linked list.  For
simplicity, assume the following structure:

  TYPE
   listptr = ^listtype;
   listtype = RECORD
     data : datatype;
     next : listptr
              END;

     When you build this structure in memory, you use the "next"
pointers to traverse the list.  When you go to write these data in a
file, however, the values in the pointer variables are irrelevant, and
can be (and should be, probably) ignored.  All of the "structure" is
in the order of the list, so simply "walk" down the list, writing each
data element (you can either write a FILE OF listtype, or to save a bit
of space, write a FILE OF datatype and just ignore the pointers) to the
file.

     When you later go to read in the file, you will, of course, need to
rebuild your data structure, here assumed to be a linked list.  This is
quite easy, as you get the elements one at a time from the file (you
wrote them in order, so you "naturally" end up reading them in order)
and append them to the growing end of your list.  [It is, of course,
easier to append to the beginning of a list than to the end, but that's
a minor detail ...].  [I'm speaking as a purist here -- I'm quite aware
of how to append to the end of a list.  My comment before refers to the
simpler recursive description of "stick-in-front" as opposed to
"append"].

     Hope this (partially) answers your question(s).

Bob Schor
Pascal Enthusiast

Re:Writing pointer records to file


I have read both those articles, but i still dont understand what in the
code made you think that i have serious troubles with the basics of pointers
(which is completely true).  Is it my use of arrays (should i be using
linked lists or something like that)?

Could you point out to me some more sites with information on pointers?

Quote
Ing. Franz Glaser wrote in message <36EB999E.919AA...@eunet.at>...
>Terence Siganakis wrote:

>> I am having some problems getting a pointer record to add itself to a
file.
>> All the write statements are Type Mismatches.......

>> ==Declaration Stuff
>> type
>> rpntr = ^recdata;
>>     recdata = record
>>     Name: string;
>>         kName: array[1..30] of string;
>>    kScore: array[1..30] of integer;
>>    Numrec: integer;
>> end;

>> Var
>>     fdat: file of rpntr; <===  I suspect the problem lies here
>>     db: array[1..5] of rpntr;

>Obviously you have serious troubles with the basics about pointers.

>Instead of giving an answer to the particular question: please read
>following articles:
>http://www.geocities.com/SiliconValley/2926/tpf.html
>page FAQ, articles "Turbo Pascal memory considerations, POINTER PRIMER"
>and "Dynamic arrays".
>--
>Franz Glaser, Glasau 3, A-4191 Vorderweissenbach Austria ++43-7219-7035
>Muehlviertler Elektronik Glaser. Industrial control and instrumentation
>-----------------------------------------------------------------------
>http://members.eunet.at/meg-glaser           mailto:meg-gla...@eunet.at
>http://www.geocities.com/~franzglaser  http://members.xoom.com/f_glaser

Re:Writing pointer records to file


when i do that it seems to run ok then i get a Stack Fault... it doesnt
write anythign to the file....

Quote
pipavi wrote in message <7cg835$5b...@news.kabelfoon.nl>...
>It must be file of recdata, or else you''l try to put the pointer in the
>memory. Then it must become write(fdat,db[dbi]^) too
>Terence Siganakis heeft geschreven in bericht
><7cfqma$kp...@m2.c2.telstra-mm.net.au>...
>>Hi,

>>I am having some problems getting a pointer record to add itself to a
file.
>>All the write statements are Type Mismatches.......

>>==Declaration Stuff
>>type
>>rpntr = ^recdata;

<snip>

Re:Writing pointer records to file


dont worry.. all fixed now...

Re:Writing pointer records to file


In article <7ci4kh$4i...@m2.c2.telstra-mm.net.au>, Terence Siganakis
<t...@vic.bigpond.net.au> writes

Quote
>dont worry.. all fixed now...

Great! But a good FAQ point anyway (I get this question many times from
students). What you appeared to be doing was writing pointers to a file
of pointers (which you can do, but there's not much point - in both
senses).

Quote
>> rpntr = ^recdata;
>> recdata = record
       <stuff>
>> end;

>> Var
>>     fdat: file of rpntr; <===  I suspect the problem lies here

Yep - you're writing pointers instead of data. Remember:

1) You need to be writing recdata to a file of recdata, not rpntr to a
file of rpntr.

2) There is not much point in saving current pointer information to a
file, even WITH the data, because pointer data is transient. Better to
work on a strategy for reconstructing the linked structure as you reload
the file. An example of this is in saving data (and data only) from an
ordered binary tree via a preorder traversal, enabling you later to
reconstruct the same tree in memory using the same method you used to
construct the tree in the first instance. This is textbook stuff but
many "homebrew" structures will yield to the same treatment.

3) This only works with relatively small data sets, i.e. those which can
reasonably be expected to fit into available memory. Anything bigger and
you ought to be looking into file-based record placement strategies - or
DB extensions for an easy life.

--
Marcus Morris - South Croydon, LONDON, UK (Mar...@ntos.demon.co.uk)

Other Threads