files organisation sequential files. readings u schneider chapter 8 u shelly cashman 1997 9.2 to...

25
Files Organisation sequential files

Upload: janice-oconnor

Post on 17-Dec-2015

220 views

Category:

Documents


0 download

TRANSCRIPT

Files Organisation

sequential files

Readings

Schneider Chapter 8 Shelly Cashman 1997 9.2 to 9.14; 1995

9.4 to 9.11 Meyer 1997 2-29 to 2-37; 1995 123 to

134 Study Book Module 13

FILE ORGANISATION

data file is ike any other file on the disk follows dos naming rules

– 8 characters followed by 3 letter extension e.g. logger1.dat

files are organised into records each of which contain data elements or fields that are related

Q12345678 john smith pass <e>

– field - an item of data that is part of a record

– record - a data structure that consists of one or more logically related fields

– key (key field) - a field in a record used as the identifying field, unique to each record

RECORD

field #1 field #2 field #3 field terminator

(optional)

Types of Files

sequential file - a file in which records are stored, read and processed on order - from the first record to the last

random access files

To Create a File

OPEN “path/filename” FOR OUTPUT AS #filenumber

e.g

OPEN “a:\carinfo\fuelusa.dat” FOR OUTPUT AS #1

OPEN “c:\carinfo\fuelaus.dat” FOR OUTPUT AS #2

The OPEN statement path - the location of the file filename - the name and extension of the

file– drive\directory\sub directory\...\filename– c:\e0001\data.bas; a:\work\logger1\temp.*

#filenumber - number between 1 and 255 that you select to identify the file

more than one file open at a time filenumber refers to specific file until it is

closed, can be reopened with another number

Putting records in the file

WRITE #filenumber, expressionlist #filenumber is the filenumber specified

in a previous OPEN statement expressionlist is the values to be written

to the file

e.g. WRITE #2, Stocknames$, numheld, price

Closing the file

CLOSE [#filenumber], [#filenumber] tells QB finished with file ends association between filename and

filenumber

e.g. CLOSE #1,#3

Example building a file start

create an empty file

get a record

WHILE validdata to enter

write the record

get new dataclose file

end

Fuel consumption problemDIM style AS STRINGDIM fuel AS SINGLEOPEN “a:\fuelcon.dat” FOR OUTPUT AS #1CLSPRINT “enter comma to quit”INPUT “style and fuel”; style, fuelDO WHILE style <> ““WRITE #1, style, fuelINPUT “style and fuel”; style, fuel

LOOPCLOSE #1

Task

rewrite the previous program eliminating the need for the first INPUT statement

PRINT “enter comma to quit”INPUT “style and fuel”; style, fuel

DO WHILE style <> ““

checking the file’s contents

use Qbasic editor to open the file– FILE, OPEN, PATH, name & extension– NB qbasic automatically looks for *.BAS

files, modify if necessary write a program to look at the file

Adding records to a file

OPEN “path/filename” FOR APPEND AS #filenumber

path/filename -exisiting directory and file name, if file does not exist it will be created

WRITE new records, will automatically go on end

CLOSE file before you can read from it

Reading DATA from a File

OPEN “path/filename” FOR INPUT AS #filenumber

INPUT #filenumber, variablelist is used to read data (in) from a file:

e.g. INPUT #2, stockname$, numheld, price

reads from file number 2 the next three fields and assigns them to stocknames$, numheld, price.

Detecting the End of the File The EOF function: EOF(filenumber)

– stands for End Of File– indicates if all data in file was read– returns TRUE value when last record in file is read and

FALSE if not e.g. EOF(2) when last record of file number 2 is read EOF is

set TRUE use with DO LOOPs

Reading Data from the fileOPEN file

WHILE not EOF get record with INPUT

Process record

LOOP

CLOSE file

Fuel Consumtion ProblemDIM make AS STRINGDIM fuel AS SINGLEOPEN “a:\fuelcon.dat” FOR INPUT AS #1

CLSPRINT “...” ‘headingsDO WHILE NOT EOF(1)INPUT #1 make, fuelPRINT make, fuel

LOOPCLOSE #1

Task

rewite program using pretest DO UNTILPRINT “...” ‘headingsDO WHILE NOT EOF(1)INPUT #1 make, fuelPRINT make, fuel

LOOPCLOSE #1

Summary of OPEN

The OPEN statement:

OPEN “path/filename” FOR mode AS #filenumber

path - the location of the file– drive\directory\sub directory\...\filename– c:\e0001\data.bas– a:\work\logger1\temp.*

MODE - output, input, append

OUTPUT - a file with the specified name will be created. The program can write data to the file. – warning: if a file with that name already exists on the

disc, the existing data will be erased and replaced by the new data.

INPUT - The file exists and will be read. If a file with the name specified does not exist, QB displays an error message “File not found” and the program stops.

APPEND - The file exists and records will be added to the end of it. If the file specified does not exist, QB creates it.

The WRITE statement:

puts the data in the file:

WRITE #filenumber, expressionlist #filenumber is the filenumber specified

in a previous OPEN statement expressionlist is the values to be written

to the file

e.g. WRITE #2, Stocknames$, numheld, price

The CLOSE statement:

CLOSE [#filenumber], [#filenumber] tells QB finished with file ends association between filename and

filenumber

e.g. CLOSE #1,#3

The INPUT statement (for files):

is used to read data (in) from a file:

INPUT #filenumber, variablelist

e.g. INPUT #2, stockname$, numheld, price

reads from file number 2 the next three fields and assigns them to stocknames$, numheld, price.

The INPUT$ statement:

returns a string of characters read from a specified file:

INPUT$ ( n ( , [#] filenumber% ] ) n: is the number of characters to read #filenumber: is the number of the opened

file. If it is omitted INPUT$ reads from the keyboard

e.g. INPUT$(1,1) - reads one character from file number 1