Board index » delphi » HELP: Writing & Reading RECORDs

HELP: Writing & Reading RECORDs

I am taking a course that includes a unit on file structures using
Pascal (that is the new of the book also). The book has an example,
but it doesn't work with TP7. I have tried what my very limited
knowledge of Pascal says to try and it still won't work. Would someone
please share a working example of WRITE/READ of RECORDs to a file?
Below is the example from the book that doesn't work.

Thanks.

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
TYPE
    Names =
        Record
            last    : Packed Array [1..15] of Char;
            first   : Packed Array [1..15] of Char;
            middle  : Char
        End;
    Addresses =
        Record
            home    : Packed Array [1..50] of Char;
            campus  : Packed Array [1..50] of Char
        End;
    Phone =
        Record
            area_code  : Integer;
            exchange   : Integer;
            number     : Integer
        End;
    Class =
        Record
            course_number : Packed Array [1..6] of Char;
            room_number   : Packed Array [1..4] of Char;
            meeting_time  : Integer
        End;
    Student =
        Record
            name         : Names;
            address      : Addresses;
            phone_number : Phone;
            classes      : Array [1..6] of Class
        End;
VAR
   student_file : File of Student;
   one_student  : Student;
   i, j         : Integer;
Begin
   Rewrite (student_file);
   While Not Eof Do     { Eof(Input) }
   Begin                { input info on one student }
       With one_student Do
       Begin
           With name Do
           Begin
               For i := 1 to 15 Do       { i : Integer }
                   Read (last[i]);
               For i := 1 To 15 Do
                   Read (first[i]);
                   Read(middle)
               End;                 { with name }

               With address Do
               Begin
                   For i := 1 To 50 Do
                       Read (home[i]);
                   For i := 1 To 50 Do
                       Read (campus[i])
                   End;                  { with address }
               With phone_number Do
                   Read (area_code, exchange, number);
               For i := 1 To 6 Do
                   With classes [i] Do
                   Begin
                       For j := 1 To 6 Do
                           Read (course_number [j]);
                       For j := 1 To 4 Do
                           Read (room_number [j]);
                       Read (meeting_time)
                   End                       { with classes [i] }
               End;                          { with one_student }
               {
               store one student's data in file buffer variable
               }
               student_file^ := one_student;               {THIS IS
WHERE THE FIRST ERROR OCCURS}
                   { write one student's data in file }
               Put (student_file) {THIS IS WHERE THE SECOND ERROR
OCCURS}
            End;                 { while }
            Close (student_file)
        End.

 

Re:HELP: Writing & Reading RECORDs


In article <42qdbi$...@ns.interserf.net>, smat...@InterserF.net says...
s>
s>Would someone
s>please share a working example of WRITE/READ of RECORDs to a file?
s>Below is the example from the book that doesn't work.
What a book??

s>...(student definition deleted)...
s>VAR
s>   student_file : File of Student;
s>   one_student  : Student;
s>   i, j         : Integer;
s>Begin
s>   Rewrite (student_file);
s>   While Not Eof Do     { Eof(Input) }
s>   Begin                { input info on one student }
s>       With one_student Do begin
s>           With name Do
You can also write "with one_student.name do begin"
s>...(student record filling procedure deleted)...
s>               student_file^ := one_student;               {THIS IS
s>WHERE THE FIRST ERROR OCCURS}
s>                   { write one student's data in file }
s>               Put (student_file) {THIS IS WHERE THE SECOND ERROR
s>OCCURS}
Yes. Write to a file with "write", here
Write(student_file,one_student);
Put is only for objects and streams.

This should help.
--
Henrik Haftmann           ||email: henrik.haftm...@e-technik.tu-chemnitz.de
09126 Chemnitz            ||  www: http://www.tu-chemnitz.de/~heha
Reichenhainer Str. 51/105 || talk: he...@grill.csn.tu-chemnitz.de

Re:HELP: Writing & Reading RECORDs


Quote
Scott Mattes (smat...@InterserF.net) wrote:

: I am taking a course that includes a unit on file structures using
: Pascal (that is the new of the book also). The book has an example,
: but it doesn't work with TP7. I have tried what my very limited
: knowledge of Pascal says to try and it still won't work. Would someone
: please share a working example of WRITE/READ of RECORDs to a file?
: Below is the example from the book that doesn't work.

: Thanks.

: =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
: TYPE
:     Names =
:         Record
:             last    : Packed Array [1..15] of Char;
:             first   : Packed Array [1..15] of Char;
:             middle  : Char
:         End;
:     Addresses =
:         Record
:             home    : Packed Array [1..50] of Char;
:             campus  : Packed Array [1..50] of Char
:         End;
:     Phone =
:         Record
:             area_code  : Integer;
:             exchange   : Integer;
:             number     : Integer
:         End;
:     Class =
:         Record
:             course_number : Packed Array [1..6] of Char;
:             room_number   : Packed Array [1..4] of Char;
:             meeting_time  : Integer
:         End;
:     Student =
:         Record
:             name         : Names;
:             address      : Addresses;
:             phone_number : Phone;
:             classes      : Array [1..6] of Class
:         End;
: VAR
:    student_file : File of Student;
:    one_student  : Student;
:    i, j         : Integer;
: Begin

  It seems to me that you need to Assign the student_file first.  Try
     Assign(student_file,'C:\STUDFILE.TXT');   or whatever the file name is

:    Rewrite (student_file);
:    While Not Eof Do     { Eof(Input) }
:    Begin                { input info on one student }
:        With one_student Do
:        Begin
:            With name Do
:            Begin
:                For i := 1 to 15 Do       { i : Integer }
:                    Read (last[i]);
:                For i := 1 To 15 Do
:                    Read (first[i]);
:                    Read(middle)
:                End;                 { with name }

:                With address Do
:                Begin
:                    For i := 1 To 50 Do
:                        Read (home[i]);
:                    For i := 1 To 50 Do
:                        Read (campus[i])
:                    End;                  { with address }
:                With phone_number Do
:                    Read (area_code, exchange, number);
:                For i := 1 To 6 Do
:                    With classes [i] Do
:                    Begin
:                        For j := 1 To 6 Do
:                            Read (course_number [j]);
:                        For j := 1 To 4 Do
:                            Read (room_number [j]);
:                        Read (meeting_time)
:                    End                       { with classes [i] }
:                End;                          { with one_student }
:                {
:                store one student's data in file buffer variable
:                }
:                student_file^ := one_student;               {THIS IS
: WHERE THE FIRST ERROR OCCURS}
:                    { write one student's data in file }
:                Put (student_file) {THIS IS WHERE THE SECOND ERROR
: OCCURS}
:             End;                 { while }
     yes, write(student_file,one_student);   would seem to be the correct
way to do this..

:             Close (student_file)
:         End.

  TayX

Re:HELP: Writing & Reading RECORDs


Quote
>I am taking a course that includes a unit on file structures using
>Pascal (that is the new of the book also). The book has an example,
>but it doesn't work with TP7. I have tried what my very limited
>knowledge of Pascal says to try and it still won't work. Would someone
>please share a working example of WRITE/READ of RECORDs to a file?
>Below is the example from the book that doesn't work.
>Thanks.

  [Code removed]

Quote
>               student_file^ := one_student;               {THIS IS
>WHERE THE FIRST ERROR OCCURS}
>                   { write one student's data in file }
>               Put (student_file) {THIS IS WHERE THE SECOND ERROR
>OCCURS}

     Several responders noted that "write(student_file, one_student)" is the
correct (Borland) syntax.  However, the above code is, to my knowledge,
correct Standard Pascal, and works for me and my compiler (for whatever that
is worth).  "Get" and "Put" are supposed to be the "elementary" i/o functions,
and should (according to the Standard) work with any file type.

Bob Schor
Pascal Enthusiast

Other Threads