Lesson+8

**LESSON 8** //**Datafiles: sn, open, close, eof**// Datafiles are secondary files which are used either to input stored data into a program or to output processed information for later use. Using datafiles consists of two separate steps: 1) Creating the datafile, and; 2) Import or exporting information from or to the file.

**//Creating Data Files//** Data files usually consist of unprocessed data. For example a set of names or ages, i.e., Joe 17 Jean 12 Jacques 14 Jennifer 23. Each entry must be separated from the next by a space. Each set of entries, such as Jim 15 is called a record. Enter the above records into a file in Turing, and save it using a .t or .txt suffix.

**//Importing Data Files//** Importing datafiles occurs in a number of discrete steps. 1) declaring a streamnumber. %A Stream number is what Turing calls the link (or stream) created to pass information from one file to another.  ﻿ 2) Open the stream number. 3) assert that there is actually data to be imported. 4) get the data from the data file. 5) close the stream number. Try this:

**var name: string** **var sn, hours: int** **var rate, grosspay:real** **rate:= 6.35** **put " Appropriate Title"** **put skip** **put "Name" : 15, "Hours":15, "Rate":15, "Gross Pay"** <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace; font-size: 17px; line-height: 25px; margin: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">** open: sn, "test.dat", get ** <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace; font-size: 17px; margin: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">** assert sn > 0 ** <span style="font-family: 'Courier New',Courier,monospace; font-size: 17px; margin: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">**loop** <span style="font-family: 'Courier New',Courier,monospace; font-size: 20px; margin: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">** get: sn, name, hours** <span style="font-family: 'Courier New',Courier,monospace; font-size: 20px; margin: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">** get: sn, skip** <span style="font-family: 'Courier New',Courier,monospace; font-size: 20px; margin: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">** exit when eof (sn) ** <span style="font-family: 'Courier New',Courier,monospace; font-size: 20px; margin: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">**grosspay:= hours * rate** <span style="font-family: 'Courier New',Courier,monospace; font-size: 20px; margin: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">**put name: 15, hours :15, rate: 15, grosspay:15** <span style="font-family: Arial,Helvetica,sans-serif; font-size: 16px; line-height: 23px; margin: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">**<span style="font-family: 'Courier New',Courier,monospace; font-size: 21px;">end loop **

<span style="font-family: Arial,Helvetica,sans-serif; font-size: 16px; line-height: 23px; margin: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">**//Output to data file//** Output to data file is very similar to input from a data file except for one thing: where one uses "get" to retrieve data, one now uses "put" instead.

<span style="font-family: Arial,Helvetica,sans-serif; font-size: 16px; line-height: 23px; margin: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">//** Task: **//

<span style="color: #000080; font-family: Arial,Helvetica,sans-serif; font-size: 16px; margin: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">1) Write a program that creates, and opens a file containing Name, Student number, and Gradeaverage. This program should display the data centred on the screen.

<span style="color: #000080; font-family: Arial,Helvetica,sans-serif; font-size: 16px; margin: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">2) Modify 1). By accident all of the student numbers begin with the digits 87. They should begin with 89. Read the file, correct the student numbers, and rewrite the file.

<span style="color: #000080; font-family: Arial,Helvetica,sans-serif; font-size: 16px; margin: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">3a) Ottawa-Carleton Region Police want a list of those criminals who have been convicted of Theft, Homicide, and Arson. The file created should contain the Name, Offence and Conviction Date of each criminal. The offences are to be listed as letter codes T for theft, A for Arson, etc. The output to screen should list the offences written out in full, i.e.,

<span style="color: #000080; font-family: Arial,Helvetica,sans-serif; font-size: 16px; margin: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">O.C.R.P.D. Criminal Listings
 * <span style="color: #000080; display: block; font-family: Arial,Helvetica,sans-serif; font-size: 16px; text-align: center;">Name || <span style="color: #000080; display: block; font-family: Arial,Helvetica,sans-serif; font-size: 16px; text-align: center;">Offence || <span style="color: #000080; display: block; font-family: Arial,Helvetica,sans-serif; font-size: 16px; text-align: center;">Conviction Date ||
 * <span style="color: #000080; font-family: Arial,Helvetica,sans-serif; font-size: 16px; margin: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">Jones, Jim || <span style="color: #000080; font-family: Arial,Helvetica,sans-serif; font-size: 16px; margin: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">Theft || <span style="color: #000080; font-family: Arial,Helvetica,sans-serif; font-size: 16px; margin: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">April 19, 200 ||
 * <span style="color: #000080; font-family: Arial,Helvetica,sans-serif; font-size: 16px; margin: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">Adams, Jane || <span style="color: #000080; font-family: Arial,Helvetica,sans-serif; font-size: 16px; margin: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">Arson || <span style="color: #000080; font-family: Arial,Helvetica,sans-serif; font-size: 16px; margin: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">January 15, 1999 ||

<span style="color: #000080; font-family: Arial,Helvetica,sans-serif; font-size: 16px; margin: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">3b) Modify 3a) such that it is menu driven and allows the user to choose between various listings.

SELECT AN OPTION 1) MASTER LISTING 2) ARSON 3) THEFT 4) HOMICIDE 5) ENTER NEW CONVICT 6) EXIT

<span style="color: #000080; font-family: Arial,Helvetica,sans-serif; font-size: 16px; margin: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">As you can see by the menu, the real challenge here is to permit the user to enter new convicts into the database without overwriting existing entries. Check out //open: sn, "filename", put// but I warn you... it's tricky.

<span style="color: #000080; font-family: Arial,Helvetica,sans-serif; font-size: 16px; margin: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;">3c) Same as 3b except with a graphical user interface <span style="font-family: Arial,Helvetica,sans-serif;">**Counters, accumulators, and the ability to use datafiles are the essential ingredients to database management. One hint on 8.3.b: explore the uses of //seek// and //mod// when using a datafile.**