Readfile problem 
Author Message
 Readfile problem

Hello,

Have a problem with a procedue in a program i'm making.

The program is to input the money you have used and
give a status etc..

If anybody have a program like this, i would like to
see the source to learn.

This procedure manages to stop the whole computer and
i cant figure out whats wrong..

The f_use file is a file of a pointer record.

PROCEDURE readuse (VAR number : Word );
BEGIN
     {$I-}
     Assign(f_use,'MONEY.MDF') ;
     Reset(f_use) ;
     IF IOResult <> 0 THEN
        BEGIN
             warning(20,7,4,'File ''MONEY.MDF'' not found!');
             Rewrite(f_use) ;
        END;
     {$I+}
     i1 := 0 ;
     WHILE NOT Eof(f_use) DO
           BEGIN
                New(userec[i1]);
                read(f_use,userec[i1]) ;
                i1 := i1 + 1 ;
           END;
     number := i1 ;
END;

Kasper.



Sun, 01 Aug 1999 03:00:00 GMT  
 Readfile problem

How about showing us your data definitions and declarations?  This
snippet of code, while a proper thing to do whenever posting, doesn't
show enough - for instance, you sure had better have declared the array
you're reading into as 0-based...

Also, how much Heap do you have available?

Lastly, I see a syntax error, meaning this hasn't been compiled: you're
not using a pointer reference in the Read statement, and you've just
allocated a pointer to where you intend to read the data record - what
gives?

Of course, you've got a _major_ problem if you don't find the file when
you're opening it - how can you expect to read from a file you've just
opened with ReWrite?  (Hint: you can't...)

Quote:
> Have a problem with a procedue in a program i'm making.
> The program is to input the money you have used and
> give a status etc..
> If anybody have a program like this, i would like to
> see the source to learn.
> This procedure manages to stop the whole computer and
> i cant figure out whats wrong..
> The f_use file is a file of a pointer record.
> PROCEDURE readuse (VAR number : Word );
> BEGIN
>      {$I-}
>      Assign(f_use,'MONEY.MDF') ;
>      Reset(f_use) ;
>      IF IOResult <> 0 THEN
>         BEGIN
>              warning(20,7,4,'File ''MONEY.MDF'' not found!');
>              Rewrite(f_use) ;
>         END;
>      {$I+}
>      i1 := 0 ;
>      WHILE NOT Eof(f_use) DO
>            BEGIN
>                 New(userec[i1]);
>                 read(f_use,userec[i1]) ;
>                 i1 := i1 + 1 ;
>            END;
>      number := i1 ;
> END;



Sun, 01 Aug 1999 03:00:00 GMT  
 Readfile problem

Quote:

> Have a problem with a procedue in a program i'm making.

You did not state for which Pascal dialect.
As you are trying non-standard features (cf. infra),
information about the particular Pascal implementation is crucial.

Quote:
> The f_use file is a file of a pointer record.

I cannot imagine what you mean by "a pointer record".
You should give the full TYPE and VAR declarations pertaining
to f_use, and userec.

Generally, a pointer is meaningful only in the particular
program incarnation (aka run) that has created it. Hence,
it is illegal to use any pointer read from a file.
The best advice I can give is not to write pointer values
to a file, at all.

Quote:
>                New(userec[i1]);

Now, what is this supposed to mean?

You cannot allocate a component of an array. Array elements
are located at contiguous, or uniformly spaced, memory locations.
In contrast, New allocates memory at an arbitrary location.

In Pascal, as in most programming languages, you cannot dynamically
extend an array, either (I guess, this is the purpose of the con-
struct above). You will have to use one of these methods:

- Keep all your records in a file. (However, you can access the
  components of a file only sequentially; hence, you will probably
  have to use several files, and shuffle the records hither and
  thither.)

- Declare an array large enough to hold more than the expected
  number of records; use it only partially (keeping the number of
  actually used elements in a separate variable, such as i1, above);
  if there are more elements to be processed than the array can hold,
  either terminate the programm indicating an error, or store the
  excessive records in a file (as above).

- Keep the records in a linked list, or any other suitable structure
  based on pointers. Allocate space for the records dynamically, as
  required (using New). Note that the program may be terminated if
  there is not enough memory available to hold all records you want
  to process; of course, you can always store part of the records
  in a file, as above.

best wishes,
   Otto Stolz



Mon, 02 Aug 1999 03:00:00 GMT  
 Readfile problem

Quote:

>This procedure manages to stop the whole computer and
>i cant figure out whats wrong..

>The f_use file is a file of a pointer record.

>PROCEDURE readuse (VAR number : Word );
>BEGIN
>     {$I-}
>     Assign(f_use,'MONEY.MDF') ;
>     Reset(f_use) ;
>     IF IOResult <> 0 THEN
>        BEGIN
>             warning(20,7,4,'File ''MONEY.MDF'' not found!');
>             Rewrite(f_use) ;

Why create an empty file?  When you try to read from this file you are going to
get "Error 104: File not open for input".  Wouldn't it be better to use Halt to
stop the program?  

Quote:
>        END;
>     {$I+}
>     i1 := 0 ;
>     WHILE NOT Eof(f_use) DO
>           BEGIN
>                New(userec[i1]);
>                read(f_use,userec[i1]) ;

You may have allocated a new record into which you play to read the record, by
alas, you don't read into the allocated memory, (userec[i1]^), but into the
pointer variable itself!  You don't want to read into userec[i1], but into the
memory it points to, e.g.  userec[i1]^

    ...red

--
Roger E. Donais
Jacksonville, FL

http://users.southeast.net/~rdonais/index.html



Mon, 02 Aug 1999 03:00:00 GMT  
 Readfile problem

Quote:


> > Have a problem with a procedue in a program i'm making.

> You did not state for which Pascal dialect.
> As you are trying non-standard features (cf. infra),
> information about the particular Pascal implementation is crucial.

> > The f_use file is a file of a pointer record.

> I cannot imagine what you mean by "a pointer record".
> You should give the full TYPE and VAR declarations pertaining
> to f_use, and userec.

> Generally, a pointer is meaningful only in the particular
> program incarnation (aka run) that has created it. Hence,
> it is illegal to use any pointer read from a file.

Technically, it is legal, if not very useful.
Quote:
> The best advice I can give is not to write pointer values
> to a file, at all.

> >                New(userec[i1]);

> Now, what is this supposed to mean?

> You cannot allocate a component of an array. Array elements
> are located at contiguous, or uniformly spaced, memory locations.
> In contrast, New allocates memory at an arbitrary location.

[Snip

Well, actually you can, if you have an array of pointers. For example,

HugeRecType =
  record
     {fields, etc.}
  end;
PtrHugeRecType = ^HugeRecType;
ArrayType = array [0..100] of PtrHugeRecType;

This would be useful if you have records of say, 1000 bytes, and are
have a guaranteed maximum amount.  While maintaining random access,
you would not waste large amounts of memory on unused records, only
a few hundred bytes of pointers.

However, moving back to the original post....

Quote:

>                 New(userec[i1]);
>                 read(f_use,userec[i1]) ;

The main problem was not that New cannot be used with arrays, but that
pointers were saved to the file rather than records.

Of couse, since |nf|n|t| (?) never posted their declarations, it's hard
to tell what this was intended to do.




Tue, 03 Aug 1999 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Please help with this problem!!ARRAY problem

2. Paradox problem on the NET - problem

3. Problems with decimals and anonther small problem

4. Memory problems (heap problems????)

5. DBGrid problem - difficult problem

6. Problem: BDE and Delphi5's ISX (Was: Re: Problem with Delphi 3 and ISX 1.11 on Windows NT / 2000)

7. KojakVGA problems (of course I could mail this to Scott Tunstall, but maybe so here knows the problem)

8. PROBLEM: Has this problem come up before? (Install Shield)

9. Borland Database Engine Locking Problem!!!! - bde problem.doc (1/1)

10. Borland Database Engine Locking Problem!!!! - bde problem.doc (0/1)

11. Date problem

12. MSAccess ODBC Windows2000: The network problem

 

 
Powered by phpBB® Forum Software