Arrays of Records and Sequential File Reading 
Author Message
 Arrays of Records and Sequential File Reading

Post the appropriate part of your code here and let's see what we can do.
It'll make a change from all this cross-posted wibble about Ada vs C++
(or ADA vs fortran, as one participant would have it).

Mark Morgan Lloyd

[Opinions above are the author's, not those of his employers or
colleagues]



Sat, 11 Apr 1998 03:00:00 GMT  
 Arrays of Records and Sequential File Reading

While searching this newsgroup, I read some questions regarding TopSpeed, so I
figured I would pose a question hoping that someone may be able to share some
information.

I am very new at working with TopSpeed and all of my external resources about
Modula-2 are variants of the Modula-2 language, consequentially I am searching
for reference or any type of help for accessing data files and reading them into
an array of a record. (ie. I have a record that will contain four fields and for
each record, I want to read into an array, so that I may recall each field seperatly
in accordance to the array that it is in.)  Following examples from a couple of books:
Programming and Problems Solving in Modula-2, and Introducing Computer Science with
Modula-2 my coding should work to a tee.  I do not have any comilation errors, just
errors in writing a field from the second array.  The program will access the second
field of the first array.

Any ideas?

I am in a rush right now so if you need more clarification, let me know.

Thanks in advance.

Robert.




Sat, 11 Apr 1998 03:00:00 GMT  
 Arrays of Records and Sequential File Reading

Quote:

>Post the appropriate part of your code here and let's see what we can do.
>It'll make a change from all this cross-posted wibble about Ada vs C++
>(or ADA vs Fortran, as one participant would have it).

>Mark Morgan Lloyd

>[Opinions above are the author's, not those of his employers or
>colleagues]

Thanks for responding.

I darn near have the program working.  I just have to read in a string from the keyboard and compare it to
the revelant record.  I am doing this as per following program snippet:

IF IsMatch = Str.Match (title,singlebook[x].TITL THEN
  (*print the whole RECORD of singlebook[x].????*)
 ELSE
  (*Display to user that no match was found.  Then loop back to start of program to allow another search*)

I am using 'x.' so that the whole ARRAY will be searched along with the field.
IsMatch is declared as a BOOLEAN.

Am I on the right track?

As for the actual record:

BOOK = RECORD
          TITL : Title;         (*TITL, AUTH, COPI AND DATE are an ARRAY of CHAR*)
          AUTH : Author;
          COPI : String;
          DATE : Due;           (*Eventually this will be arrayed so that I can have up to
          END;                    four Dates per book*)

Library = ARRAY [1..maxbook] OF BOOK;  (*according to all the references I read, this line should take
the four fields in record BOOK and address them per ARRAY until it reaches the maximum or before (if
the data file is shorter than maxbook)).

I found out that this is not the case, since my data file is set up like:
TITL
AUTH
COPI
DATE
    :
    :
DATE

maxbook relates to the number of lines in the data file.  

        After some work the other day, I successfully called random fields, within the program, from the
array and displayed them.  I did this by changing the 'x' to 1, 2, 3 . . . for whatever book II wanted along
with the field.  This is what I initially had problems with.  Now when I do a search from the keyboard to
find the title of a book for instance, I can pull up anything I want, which should not be.
        Is it something to do with the variable 'x' as noted above with the Match.  Or should I approach this
from a different angle.

Thanks.




Mon, 13 Apr 1998 03:00:00 GMT  
 Arrays of Records and Sequential File Reading

Quote:


> >Post the appropriate part of your code here and let's see what we can
> do. >It'll make a change from all this cross-posted wibble about Ada vs
> C++ >(or ADA vs Fortran, as one participant would have it).

> >Mark Morgan Lloyd

> >[Opinions above are the author's, not those of his employers or
> >colleagues]

YHM

Quote:

> Thanks for responding.

> I darn near have the program working.  I just have to read in a string
> from the keyboard and compare it to the revelant record.  I am doing
> this as per following program snippet:

> IF IsMatch = Str.Match (title,singlebook[x].TITL THEN
>   (*print the whole RECORD of singlebook[x].????*)
>  ELSE
>   (*Display to user that no match was found.  Then loop back to start
> of program to allow another search*)

> I am using 'x.' so that the whole ARRAY will be searched along with the
> field.
> IsMatch is declared as a BOOLEAN.

> Am I on the right track?

Not quite sure here what you mean by "I am using 'x.'" etc. Also, is that
boolean expression quite right, or do you mean IF Str.Match() ?

- Show quoted text -

Quote:
> As for the actual record:

> BOOK = RECORD
>         TITL : Title;         (*TITL, AUTH, COPI AND DATE are an ARRAY
of CHAR*)
>         AUTH : Author;
>         COPI : String;
>         DATE : Due;           (*Eventually this will be arrayed so that
I can have up
> to      END;                    four Dates per book*)

> Library = ARRAY [1..maxbook] OF BOOK;  (*according to all the
> references I read, this line should take the four fields in record BOOK
> and address them per ARRAY until it reaches the maximum or before (if
> the data file is shorter than maxbook)).

What functions are you using to write and read the file? In general, they
won't know about your data representation, so it's down to you to check
for EOF etc.

- Show quoted text -

Quote:
> I found out that this is not the case, since my data file is set up
> like:
> TITL
> AUTH
> COPI
> DATE
>     :
>     :
> DATE

> maxbook relates to the number of lines in the data file.  

>       After some work the other day, I successfully called random
fields,
> within the program, from the array and displayed them.  I did this by
> changing the 'x' to 1, 2, 3 . . . for whatever book II wanted along
> with the field.  This is what I initially had problems with.  Now when
> I do a search from the keyboard to find the title of a book for
> instance, I can pull up anything I want, which should not be.
>       Is it something to do with the variable 'x' as noted above with
the
> Match.  Or should I approach this from a different angle.

> Thanks.



Hope that's been of some help

Mark Morgan Lloyd

[Opinions above are the author's, not those of his employers or
colleagues]



Mon, 13 Apr 1998 03:00:00 GMT  
 Arrays of Records and Sequential File Reading
hi,
 be careful with Str.Match, if the string in your record
 is not terminated by CHR(0) the procedure wont stop matching.
 as someone pointed out reading and writing of the records
 might be crucial, are you doing all in memory.

engelbert

--
--------------------------------
- ASM-ISN Dept. SMC            -
- engelbert gruber             -
- peter fintl                  -
- helmut gragger               -

--------------------------------



Tue, 14 Apr 1998 03:00:00 GMT  
 Arrays of Records and Sequential File Reading

Quote:



>> >Post the appropriate part of your code here and let's see what we can
>> do. >It'll make a change from all this cross-posted wibble about Ada vs
>> C++ >(or ADA vs Fortran, as one participant would have it).

>> >Mark Morgan Lloyd

>> >[Opinions above are the author's, not those of his employers or
>> >colleagues]

>YHM

>> Thanks for responding.

>> I darn near have the program working.  I just have to read in a string
>> from the keyboard and compare it to the revelant record.  I am doing
>> this as per following program snippet:

>> IF IsMatch = Str.Match (title,singlebook[x].TITL THEN
>>   (*print the whole RECORD of singlebook[x].????*)
>>  ELSE
>>   (*Display to user that no match was found.  Then loop back to start
>> of program to allow another search*)

>> I am using 'x.' so that the whole ARRAY will be searched along with the
>> field.
>> IsMatch is declared as a BOOLEAN.

>> Am I on the right track?

>Not quite sure here what you mean by "I am using 'x.'" etc. Also, is that
>boolean expression quite right, or do you mean IF Str.Match() ?

>> As for the actual record:

>> BOOK = RECORD
>>         TITL : Title;         (*TITL, AUTH, COPI AND DATE are an ARRAY
>of CHAR*)
>>         AUTH : Author;
>>         COPI : String;
>>         DATE : Due;           (*Eventually this will be arrayed so that
>I can have up
>> to      END;                    four Dates per book*)

>> Library = ARRAY [1..maxbook] OF BOOK;  (*according to all the
>> references I read, this line should take the four fields in record BOOK
>> and address them per ARRAY until it reaches the maximum or before (if
>> the data file is shorter than maxbook)).

>What functions are you using to write and read the file? In general, they
>won't know about your data representation, so it's down to you to check
>for EOF etc.

>> I found out that this is not the case, since my data file is set up
>> like:
>> TITL
>> AUTH
>> COPI
>> DATE
>>     :
>>     :
>> DATE

>> maxbook relates to the number of lines in the data file.  

>>       After some work the other day, I successfully called random
>fields,
>> within the program, from the array and displayed them.  I did this by
>> changing the 'x' to 1, 2, 3 . . . for whatever book II wanted along
>> with the field.  This is what I initially had problems with.  Now when
>> I do a search from the keyboard to find the title of a book for
>> instance, I can pull up anything I want, which should not be.
>>       Is it something to do with the variable 'x' as noted above with
>the
>> Match.  Or should I approach this from a different angle.

>> Thanks.


>Hope that's been of some help

>Mark Morgan Lloyd

Sorry for taking so long to respond.

First of all you were right in that IsMatch := Str.Match is not the same as
Str.Match();  I did not realize that Str.Match is a boolean function.  After fixing
that bug I have moderate success with the program.  However, when I search the library,
I am using a REPEAT/UNTIL loop with my IF Str.Match(title,singlebook[x].TITLE) THEN
in between.  If a match is found then I print out the Title of the specific book,  If the
title is not found in the library, then I print out that the title is not found, but during
the search of the library until my 'x' := maxbook, I end up printing out 'book not found' until
I find a match.  I thought of changing the REPEAT loop to a FOR...NEXT loop, similar to what
Dieter. W. wrote in his response.  Will this solve the problem or should I change to a WHILE loop?

As for your last question of how I am handleing the files.  I am reading them from a text file
using FIO.RdStr, I will be writing the results back to disk once I have this program operating correctly
using FIO.WrStr and FIO.WrCard (for the Date field).

Robert. Leroux



Fri, 17 Apr 1998 03:00:00 GMT  
 Arrays of Records and Sequential File Reading

Quote:
> Str.Match();  I did not realize that Str.Match is a boolean function.  After fixing
> that bug I have moderate success with the program.  However, when I search the library,
> I am using a REPEAT/UNTIL loop with my IF Str.Match(title,singlebook[x].TITLE) THEN
> in between.  If a match is found then I print out the Title of the specific book,  If the
> title is not found in the library, then I print out that the title is not found, but during
> the search of the library until my 'x' := maxbook, I end up printing out 'book not found' until
> I find a match.  I thought of changing the REPEAT loop to a FOR...NEXT loop, similar to what

To be sure what your problem is:

When running your program you enter a title and receive a lot of
"book not found" (until you get "found" or "end of list")?

If this is not the problem, forget the rest and specify again.

   1. you decide how to search:
        the whole library (if there are serveral matches possible)
                FOR x := 1 to max.. DO ...
        only until the first match.
                REPEAT ... UNTIL isFound
   2. you do your search,
         but instead of output "not found" you set a boolean isFound
          in your search loop,
   3. you evaluate isFound after the whole search.
     if you set isFound := FALSE before your search, you must only set it
          TRUE when there is a match.

hope this helps

 Dieter Wienrich



Sat, 18 Apr 1998 03:00:00 GMT  
 Arrays of Records and Sequential File Reading

Quote:

>> Str.Match();  I did not realize that Str.Match is a boolean function.  After fixing
>> that bug I have moderate success with the program.  However, when I search the library,
>> I am using a REPEAT/UNTIL loop with my IF Str.Match(title,singlebook[x].TITLE) THEN
>> in between.  If a match is found then I print out the Title of the specific book,  If the
>> title is not found in the library, then I print out that the title is not found, but during
>> the search of the library until my 'x' := maxbook, I end up printing out 'book not found' until
>> I find a match.  I thought of changing the REPEAT loop to a FOR...NEXT loop, similar to what

>To be sure what your problem is:

>When running your program you enter a title and receive a lot of
>"book not found" (until you get "found" or "end of list")?

>If this is not the problem, forget the rest and specify again.

>   1. you decide how to search:
>        the whole library (if there are serveral matches possible)
>                FOR x := 1 to max.. DO ...
>        only until the first match.
>                REPEAT ... UNTIL isFound
>   2. you do your search,
>         but instead of output "not found" you set a boolean isFound
>          in your search loop,
>   3. you evaluate isFound after the whole search.
>     if you set isFound := FALSE before your search, you must only set it
>          TRUE when there is a match.

>hope this helps

> Dieter Wienrich

Thanks, I managed to get it happening just after the last post using a REPEAT
. .. UNTIL loop.  I am only working at getting the ARRAY of the date field
working then I should be quite well off.  I have an idea on how to address the
Date filed and print out the dates per book depending on how many copies of the
particular book is available, but I believe using the CASE statement, I would have
to read in my 'copi' field as a CHAR so that I can compare it to the current
copi that is read into the record.  I am going to try to address this problem with
an IF...THEN statement tomorrow first, otherwise I think reading in the copi field as
a CHAR instead of a Str should solve my problem.

- Show quoted text -



Mon, 20 Apr 1998 03:00:00 GMT  
 Arrays of Records and Sequential File Reading

Quote:
>Thanks, I managed to get it happening just after the last post using a REPEAT
>.. .. UNTIL loop.  I am only working at getting the ARRAY of the date field
>working then I should be quite well off.  I have an idea on how to address the
>Date filed and print out the dates per book depending on how many copies of the
>particular book is available, but I believe using the CASE statement, I would have
>to read in my 'copi' field as a CHAR so that I can compare it to the current
>copi that is read into the record.  I am going to try to address this problem with
>an IF...THEN statement tomorrow first, otherwise I think reading in the copi field as
>a CHAR instead of a Str should solve my problem.

The purists will kill me for this, but the LOOP  END; EXITing when a hit is
made is a very useful tool.  The academics will say it's crude and you can do
the same thing with either REPEAT UNTIL or DO WHILE but there are lots of
times when one hasn't the time or the inclination to struggle through one or
the other.

Jim T.



Thu, 23 Apr 1998 03:00:00 GMT  
 Arrays of Records and Sequential File Reading

|> If you have to deal with "real world data", you often have multiple exits from
|> a loop. The easiest way to do this is LOOP .. END. All other constructs will
|> become very complicated. I think thats the reason LOOP .. END is for.

Definitely.  Over time many of my non-FOR loops get switched from WHILEs
and REPEAT/UNTILs to LOOPs.  How many times have you seen this in code?

someVar := SomeFunction();
WHILE someVar <> someSpecialValue DO
  ...whatever...
  someVar := SomeFunction();
END (*While*);

With a LOOP this nicely becomes:

LOOP
  someVar := SomeFunction();
  IF someVar = someSpecialValue THEN
    EXIT (*Loop*);
  END (*If*);

  ...whatever...
END (*Loop*);
--

- Bill Seurer     ID Tools and Compiler Development      IBM Rochester, MN



Fri, 01 May 1998 03:00:00 GMT  
 Arrays of Records and Sequential File Reading

Quote:

>The purists will kill me for this, but the LOOP  END; EXITing when a hit is
>made is a very useful tool.  The academics will say it's crude and you can do
>the same thing with either REPEAT UNTIL or DO WHILE but there are lots of
>times when one hasn't the time or the inclination to struggle through one or
>the other.

I'm pretty much of a purist, but in this case I agree with you.
In fact I code the initial version of _all_ my loops with the
LOOP construct, and there are usually multiple exits because
of things like error checking and special case checking.  Once
I've convinced myself that I've covered all cases, I look to see
whether tests can be combined.  If the end result is an exit
at the beginning or end of the block, I then change the loop
into a WHILE or REPEAT loop, as appropriate.

In my experience, programmers often have trouble with loops, for
two reasons:
 1. A real-life loop is rarely of the kind "WHILE simple-condition
    DO something".  In practice the condition is a complicated one
    because there are multiple conditions to test.  For example,
    programmers often fail to see that "WHILE p^ <> x" should have
    been written as something like "WHILE (p <> NIL) AND (p^ <> x)".
    (In Pascal it's even worse, because the standard doesn't
    specify short-circuit evaluation of conditions.)
 2. If you stick to the WHILE and REPEAT forms, then you have to
    make a decision at the outset - i.e. before coding the loop
    body - about whether the exit test should be at the beginning
    or the end.  In my opinion it's better to make that decision
    after you've worked out what should be in the body.

As a result, I believe that LOOP actually gives cleaner coding
practices - although I do believe in converting a LOOP into
something simpler, if appropriate, once I've finished the first
draft of the code.

Multiple exits don't bother me, because the exits all jump to
the same point, i.e. the code is still well-structured.

--

                           ftp://ee.newcastle.edu.au/pub/www/Moylan.html



Fri, 01 May 1998 03:00:00 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Cla2.1Skipping Records in Sequential File Loop

2. Deleting records in a sequential file

3. saving variant records in sequential files

4. Sequential File Update w/ Multiple Input Records

5. Inserting records into sequential data files

6. Skipping records in a sequential-access unformatted file

7. Help: sequential read of an indexed file defined Access is random

8. Reading sequential, unformatted data files

9. fortran program to read a unformatted sequential file

10. Reading unformatted sequential file on Sun SparcStation

11. An array of records within an array of records (Still a problem)

12. an array of records within an array of records

 

 
Powered by phpBB® Forum Software