Chapter 1
Introduction to DOS

Chapter 2
Introduction to Turbo Pascal

Chapter 3
Parts of a Pascal Program

Chapter 4
Control Structures and Looping

Chapter 5
Looping

Chapter 6
Procedures

Chapter 7
Parameters Passing

Chapter 8
Functions

Chapter 9
Arrays

Chapter 10
Searching and Sorting

Chapter 11
Records and File of Records

CHAPTER 11

( Records and File of Records )    


In Chapter 9 we saw that much programming time can be saved by the effective use of arrays. If you wanted to keep names of all your friends and their telephone numbers in the computer, you could create two arrays, one to hold the names and the other to hold their telephone numbers. Suppose "Jim Larson" was the second name in the name-list, then his telephone number should be the second one in the telephone-list. That way you can access the name and the telephone number of the same person using one index. A limitation of the array is that it can hold only one type of data. In real life applications, information related to a particular item could be composed of different data types. A Pascal structure that could accommodate different types of data and can be referenced as a single unit is the 'RECORDS' data type.

For example, if you decide to keep a record of all the checks you write, so that they can be categorized based on types of expenses, you will need to have at least these data types:

Check Number Integer

Paid to String

Amount Real

Category String or Integer

You could create four parallel arrays to hold this information. However, consider referring to all four data items by one name: Check. In Pascal this can be done by using the 'RECORDS' data type.

In Pascal this can be done by using the 'RECORDS' data type. You simply will have to define what a check is, like this:

type
    CheckType = record
    checkNumber : integer;
    paidTo : string[20];
    amount : real;
    category : integer;
end;

var
    Check, Payment : CheckType;
'Check' is considered to be a record. Each data item is called a field. Once it has been declared like this, we can store data into the record as follows:

    readln(Check.checkNumber);

    readln(Check.PaidTo);

    readln(Check.amount);

    readln(Check.category);

or as follows:

with Check do

begin

    readln(checkNumber);

    readln(PaidTo);

    readln(amount);

    readln(category);

end;

Using 'with Check do' eliminates the need for repeating 'Check.' when referring to a field. All these operations are

legal:

    readln(Check.checkNumber);

    writeln(Check.checkNumber);

    Check.checkNumber := 1233;

    Payment := Check; {see the variable declaration above.}

We can also create array of records if we wish to keep one month's checks in the computer memory:

var

    Check : array[1..50] of CheckType;

    i : integer;

begin

    for i:= 1 to 30 do

      with Check[i] do

      begin

        readln(checkNumber);

        readln(paidTo);

        readln(amount);

        readln(Category);

end;

Let's put it all together and write a program to input some check records. To minimize data entry time, the program is written to accept information on five checks.

PROGRAM 11-1

Program RecordCheck(input);

type

    CheckType = record

    checkNumber : integer;

    paidTo : string[20];

    amount : real;

    category : integer;

end;

    CheckArray = array[1..5] of CheckType;

var

    check : CheckArray;

    count : integer;

begin

    writeln('YOU WILL BE ASKED TO ENTER INFORMATION ABOUT 5 CHECKS.');

    writeln('For each check enter:');

    writeln(' check number');

    writeln(' paid to');

    writeln(' amount');

    writeln(' category (an integer)');

    writeln('each followed by the enter key.');

    writeln;

    for count := 1 to 5 do

    begin

      writeln('check :',count:3);

      with check[count] do

      begin

        readln(checkNumber);

        readln(paidTo);

        readln(amount);

        readln(category);

      end;

      writeln;

    end;

end.

Program run:

    C:\TP>turbo

    Turbo Pascal Version 6.0 Copyright (c) 1983,90 Borland International

    YOU WILL BE ASKED TO ENTER INFORMATION ABOUT 5 CHECKS.

    For each check enter:

    check number

    paid to

    amount

    category (an integer)

    each followed by the enter key.

    check : 1

    101

    HEB

    19.35

    4

    check : 2

    102

    UT PANAM

    59.35

    5

    check : 3

    4

    check : 2

    102

    UT PANAM

    59.35

    5

    check : 3

    103

    CENTRAL POWER AND LIGHT

    210.36

    10

    check : 4

    104

    MCALLEN PUBLIC UTILITIES

    22.99

    10

    check : 5

    105

    BELL TELEPHONE

    59.12

    11

    Type EXIT to return to Turbo Pascal...

    Microsoft(R) MS-DOS(R) Version 5.00

    (C)Copyright Microsoft Corp 1981-1991.

The array of records created in Program 11-1 can easily be written to a file. Such a file is called file of records. Let us write the records we just created in a file. It would be a good idea to review chapter three where we talked about text file manipulation. File of records are assigned, opened and closed exactly the same way. You can do seek operation of file of records, this will allow you to write or read any record randomly. Program 11-1 creates a record file.

PROGRAM 11-2

{This program will ask for information about 5 checks,

check number, payee, amount and category. Then it will

write it into a file of records}

Program RecordCheck(input, bank);

type

    CheckType = record

    checkNumber : integer;

    paidTo : string[20];

    amount : real;

    category : integer;

end;

CheckArray = array[1..5] of CheckType;

FileRec = file of checkType;

var

    check : CheckArray;

    count : integer;

    bank : fileRec;

begin

    assign(bank,'check.dta'); {file opening}

    rewrite(bank); {to write}

    writeln('YOU WILL BE ASKED TO ENTER INFORMATION ABOUT 5 CHECKS.'); {instructions}

    writeln('For each check enter:');

    writeln(' check number');

    writeln(' paid to');

    writeln(' amount');

    writeln(' category (an integer)');

    writeln('each followed by the enter key.');

    writeln;

PROGRAM 11-2 CONTINUED.

{actual reading of information}

    for count := 1 to 5 do

    begin

      writeln('check :',count:3);

      with check[count] do

      begin

      readln(checkNumber);

      readln(paidTo);

      readln(amount);

      readln(category);

    end;

    write(bank,check[count]); {write to file

    writeln;

end;

close(bank);

    end.

    After running the Program 11-2, exit Pascal and check the directory. You should see a file called "check.dta". If you type this file out, you will notice most characters are unreadable. Files created as file of records are not saved in ASCII format and are not readable. This is the difference between an ASCII file created by the text file mode and the record file mode.

    Now let's see how we can retrieve the information placed in a record file. Program 11-3 reads records written by the previous program and displays them on the screen.

    PROGRAM 11-3

    Program RecordCheck(input, bank);

    type

      CheckType = record

      checkNumber : integer;

      paidTo : string[20];

      amount : real;

      category : integer;

    end;

    CheckArray = array[1..5] of CheckType;

    FileRec = file of checkType;

    var

      check : CheckArray;

      count : integer;

      bank : fileRec;

    begin

      assign(bank,'check.dta');

      reset(bank);

      count :=0;

      writeln('check# Paid To Amount Category');

      writeln;

      while not eof(bank) do

      begin

        count := count+1;

        read(bank, check[count]);

        with check[count] do

        begin

          write(checkNumber:6);

          write(paidTo:24);

          write(amount:10:2);

          writeln(category:6);

        end;

      end;

      close(bank);

    end.

    Program run:

      C:\TP>

      check# Paid To Amount Category

      1312 John Abraham 114.33 10

      135 reggie 512.33 22

      5123 jones 51.22 3

      21 jacob 21.20 5

      33 mary 12.33 1

      Type EXIT to return to Turbo Pascal...

      Microsoft(R) MS-DOS(R) Version 5.00

      (C)Copyright Microsoft Corp 1981-1991.

    Here is another program that puts it all together, procedures, functions, arrays, files, iteration, etc. Here I use a menu to allow the user to choose a particular function.

    Program fileRec (input, output,telefile);

    uses crt;

    {first programming assignment for 2380 students

    1  ) Create an array of records with name and telephone number as fields
    2  ) Sort the array of records
    3 ) Write the array of records into a file of records
    4  ) Do a binary search for the name field
    5A) If match found print the name and telephone number
    5B) Edit the record if so desired
    6  ) Send the entire name and telephone list to a printer

    Use procedures and functions where appropriate. A menu driven program would be more user friendly}

    Const

      MaxPeople = 100;

      FileName = 'TELEFILE.DTA';

    Type

      PersonRec = record

      name : string[30];

      tele : string[8]

    end;

    PeopleList = array [1..maxPeople] of PersonRec;

    teleFileType = file of personRec;

    var

      People : peoplelist;

      numPeople :Integer;

      telefile : telefileType;

    (***************)

    Procedure pause;

    begin

      write('Press any key to return to the Menu..');

      while not keypressed do;

    end;

    (***************)

    Procedure CreateNewFile (var telefile : telefileType);

    begin

      Assign(telefile, filename);

      rewrite(telefile);

      writeln('New file opened.');

      pause;

    end;

    (****************)

    Procedure Menu;

    begin

      textbackground(cyan);textcolor(blue);

      clrscr;

      writeln('CHOOSE ONE OF THE FOLLOWING: ');

      writeln(' 1. CREATE A NEW FILE');

      writeln(' 2. READ AN EXISTING FILE');

      writeln(' 3. ADD NAME AND TELEPHONE NUMBERS');

      writeln(' 4. SORT THE LIST');

      writeln(' 5. WRITE THE SORTED NEW LIST TO THE FILE');

      writeln(' 6. PRINT THE ENTIRE LIST');

      writeln(' 7. SEARCH FOR AND EDIT A RECORD');

      writeln(' 0. QUIT THE PROGRAM');

      write('Your choice? ');

    end;

    (***********)

    Procedure OpenFileAndRead(var telefile:telefileType; var people :peopleList;

    var numPeople :integer);

    begin

      Assign(telefile, filename);

      reset(telefile);

      numPeople :=0;

      while not eof(telefile) do

      begin

        numPeople := numpeople +1;

        read(telefile, people[numPeople]);

      end;

      writeln('Total names read: ',numPeople:4);

      pause;

    end;

    (***************)

    Procedure InputInfo(var People:peopleList; var NumPeople: integer);

    var

      tmpName : string[30];

    begin

      writeln('This procedure will add names to the list.');

      writeln('Enter LastName, FirstName MiddleName');

      write('Please enter name (just ENTER to quit) : ');

      readln(tmpName);

      while tmpName <> '' do

      begin

        NumPeople := NumPeople + 1;

        People[numPeople].name := tmpName;

        write('Telephone number : ');

        readln(People[numpeople].tele);

        write('Please enter another name (just ENTER to quit) :');

        readln(tmpName);

      end; {while}

    end; {InputInfo}

    (*********************)

    Procedure Sort(var People: peopleList; numPeople:integer);

    {sort using bubble sort}

    var

      passNum, index : integer;

      Procedure SwapPeople (var person1, person2 : personRec);

    var

      temp : PersonRec;

    begin

      temp := person1;

      person1 := person2;

      person2 := temp;

    end; {swapPeople}

    BEGIN {sort}

      for PassNum := 1 to numPeople - 1 do

      For index := 1 to NumPeople - passNum do

      if people[index].name > people[index+1].name then

      SwapPeople(people[index],people[index+1]);

      writeln('Sort completed.');

      pause;

    end; {sort}

    (************)

    Procedure Writefile ( people : PeopleList; numPeople :integer;

    var telefile : telefileType);

    var

      count : integer;

    begin

      rewrite(telefile);

      for count := 1 to numPeople do

      write (telefile,people[count]);

      writeln;writeln('File save completed.');

      pause;

    end;

    (******************)

    Procedure Printfile ( people : PeopleList; numPeople :integer);

    var

      tmp :char;

      count : integer;

    begin

      for count := 1 to numPeople do

      begin

        with people[count] do

        writeln (name:1,' ':35-length(name), tele);

      end;

      writeln('Print completed.');

      pause;

    end;

    (******************)

    Procedure BinarySearch(var people: PeopleList; peopleCount: integer);

    var

      first, middle, last : integer;

      temp : string[30];

      found: boolean;

    begin

      found := false; first := 1; last := PeopleCount;

      write('Enter Name to search for: ');

      readln(temp);

      while (not(found)) AND (first <= last) do

        begin

        middle := (last+1 - first) DIV 2 + FIRST;

        if People[middle].name = temp then

        begin

          found := true;

          writeln('Match found: ', people[middle].name:1, people[middle].tele:10);

          write('Enter name change if any: ');readln (temp);

          if temp <> '' then people[middle].name := temp;

          write('Enter tele change if any: ');readln(temp);

          if temp <> '' then people[middle].tele := temp;

        end

        else

        begin

          if temp > People[middle].name

          then first := middle+1

          else last := middle-1;

        end;

      end;

      if not(found) then writeln('Name not in list');

      pause;

    end;

    (******************)

    var choice: integer;

    Begin

      menu; readln(choice);

      while choice > 0 do

        begin

        Case choice of

          1 : CreateNewFile(telefile);

          2 : openFileAndRead (telefile, people, numPeople);

          3 : inputinfo (people,NumPeople);

          4 : sort (people, NumPeople);

          5 : writefile(people,numPeople,teleFile);

          6 : printfile(people,NumPeople);

          7 : binarySearch(people, NumPeople);

        end;

        menu; readln(choice);

      end;

      close(telefile);

    end.

    Program run:

      C:\TP>

      CHOOSE ONE OF THE FOLLOWING:

      1. CREATE A NEW FILE

      2. READ AN EXISTING FILE

      3. ADD NAME AND TELEPHONE NUMBERS

      4. SORT THE LIST

      5. WRITE THE SORTED NEW LIST TO THE FILE

      6. PRINT THE ENTIRE LIST

      7. SEARCH FOR AND EDIT A RECORD

      0. QUIT THE PROGRAM

      Your choice?

      6

      ABRAHAM 687-2932

      BOBBY 3513

      JOHN 3422

      MERCY 993

      REGGIE 35122

      Print completed.

      Press any key to return to the Menu..

      Your choice?

      4

      CHOOSE ONE OF THE FOLLOWING:

      1. CREATE A NEW FILE

      2. READ AN EXISTING FILE

      3. ADD NAME AND TELEPHONE NUMBERS

      4. SORT THE LIST

      5. WRITE THE SORTED NEW LIST TO THE FILE

      6. PRINT THE ENTIRE LIST

      7. SEARCH FOR AND EDIT A RECORD

      0. QUIT THE PROGRAM

      Your choice?

      6

      ABRAHAM 687-2932

      BOBBY 3513

      JOHN 3422

      MERCY 993

      REGGIE 35122

      Print completed.

      Press any key to return to the Menu..


    Go to top of this chapter

    Site design/development provided by the UTPA NewMedia Center
    @1999 The University of Texas-Pan American