LESSON+9

  //**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** <span style="font-family: 'Courier New',Courier,monospace; font-size: 130%;"> **rate:= 6.35** <span style="font-family: 'Courier New',Courier,monospace; font-size: 130%;"> **put " Appropriate Title"** <span style="font-family: 'Courier New',Courier,monospace; font-size: 130%;"> **put skip** <span style="font-family: 'Courier New',Courier,monospace; font-size: 130%;"> **put "Name" : 15, "Hours":15, "Rate":15, "Gross Pay"** <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace; font-size: 130%;">** open: sn, "test.dat", get ** <span style="color: #ff0000; font-family: 'Courier New',Courier,monospace; font-size: 130%;"> ** assert sn > 0 ** <span style="font-family: 'Courier New',Courier,monospace; font-size: 130%;"> **loop**

<span style="font-family: 'Courier New',Courier,monospace; font-size: 156%;"> ** get: sn, name, hours** <span style="font-family: 'Courier New',Courier,monospace; font-size: 156%;"> ** get: sn, skip** <span style="font-family: 'Courier New',Courier,monospace; font-size: 156%;"> ** exit when eof (sn) ** <span style="font-family: 'Courier New',Courier,monospace; font-size: 156%;"> **grosspay:= hours * rate** <span style="font-family: 'Courier New',Courier,monospace; font-size: 156%;"> **put name: 15, hours :15, rate: 15, grosspay:15**

<span style="font-family: Arial,Helvetica,sans-serif; font-size: 120%;"> **<span style="font-family: 'Courier New',Courier,monospace; font-size: 130%;">end loop **

<span style="font-family: Arial,Helvetica,sans-serif; font-size: 120%;"> **//Output to data file//** <span style="font-family: Arial,Helvetica,sans-serif; font-size: 120%;"> 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: 120%;"> //** Task: **// <span style="color: #000080; font-family: Arial,Helvetica,sans-serif; font-size: 120%;"> 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: 120%;"> 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: 120%;"> 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., O.C.R.P.D. Criminal Listings
 * <span style="color: #000080; display: block; font-family: Arial,Helvetica,sans-serif; font-size: 120%; text-align: center;">Name || <span style="color: #000080; display: block; font-family: Arial,Helvetica,sans-serif; font-size: 120%; text-align: center;">Offence || <span style="color: #000080; display: block; font-family: Arial,Helvetica,sans-serif; font-size: 120%; text-align: center;">Conviction Date ||
 * <span style="color: #000080; font-family: Arial,Helvetica,sans-serif; font-size: 120%;">Jones, Jim || <span style="color: #000080; font-family: Arial,Helvetica,sans-serif; font-size: 120%;">Theft || <span style="color: #000080; font-family: Arial,Helvetica,sans-serif; font-size: 120%;">April 19, 200 ||
 * <span style="color: #000080; font-family: Arial,Helvetica,sans-serif; font-size: 120%;">Adams, Jane || <span style="color: #000080; font-family: Arial,Helvetica,sans-serif; font-size: 120%;">Arson || <span style="color: #000080; font-family: Arial,Helvetica,sans-serif; font-size: 120%;">January 15, 1999 ||

<span style="color: #000080; font-family: Arial,Helvetica,sans-serif; font-size: 120%;"> 3b) Modify 3a) such that it is menu driven and allows the user to choose between various listings. i.e., <span style="color: #000080; font-family: Arial,Helvetica,sans-serif; font-size: 120%;"> SELECT AN OPTION <span style="color: #000080; font-family: Arial,Helvetica,sans-serif; font-size: 120%;"> 1) MASTER LISTING <span style="color: #000080; font-family: Arial,Helvetica,sans-serif; font-size: 120%;"> 2) ARSON <span style="color: #000080; font-family: Arial,Helvetica,sans-serif; font-size: 120%;"> 3) THEFT <span style="color: #000080; font-family: Arial,Helvetica,sans-serif; font-size: 120%;"> 4) HOMICIDE <span style="color: #000080; font-family: Arial,Helvetica,sans-serif; font-size: 120%;"> 5) ENTER NEW CONVICT <span style="color: #000080; font-family: Arial,Helvetica,sans-serif; font-size: 120%;"> 6) EXIT

<span style="color: #000080; font-family: Arial,Helvetica,sans-serif; font-size: 120%;"> 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: 120%;"> 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 9.3.b explore the uses of //seek// and //mod// when using a datafile.**