Presentation is loading. Please wait.

Presentation is loading. Please wait.

Files Organisation sequential files. Readings u Schneider Chapter 8 u Shelly Cashman 1997 9.2 to 9.14; 1995 9.4 to 9.11 u Meyer 1997 2-29 to 2-37; 1995.

Similar presentations


Presentation on theme: "Files Organisation sequential files. Readings u Schneider Chapter 8 u Shelly Cashman 1997 9.2 to 9.14; 1995 9.4 to 9.11 u Meyer 1997 2-29 to 2-37; 1995."— Presentation transcript:

1 Files Organisation sequential files

2 Readings u Schneider Chapter 8 u Shelly Cashman 1997 9.2 to 9.14; 1995 9.4 to 9.11 u Meyer 1997 2-29 to 2-37; 1995 123 to 134 u Study Book Module 13

3 FILE ORGANISATION u data file is ike any other file on the disk u follows dos naming rules –8 characters followed by 3 letter extension e.g. logger1.dat u files are organised into records each of which contain data elements or fields that are related

4 Q12345678 john smith pass –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)

5 Types of Files u sequential file - a file in which records are stored, read and processed on order - from the first record to the last u random access files

6 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

7 The OPEN statement u path - the location of the file u filename - the name and extension of the file –drive\directory\sub directory\...\filename –c:\e0001\data.bas; a:\work\logger1\temp.* u #filenumber - number between 1 and 255 that you select to identify the file u more than one file open at a time u filenumber refers to specific file until it is closed, can be reopened with another number

8 Putting records in the file WRITE #filenumber, expressionlist u #filenumber is the filenumber specified in a previous OPEN statement u expressionlist is the values to be written to the file e.g. WRITE #2, Stocknames$, numheld, price

9 Closing the file CLOSE [#filenumber], [#filenumber] u tells QB finished with file u ends association between filename and filenumber e.g. CLOSE #1,#3

10 Example building a file start create an empty file get a record WHILE valid data to enter write the record get new data close file end

11 Fuel consumption problem DIM style AS STRING DIM fuel AS SINGLE OPEN “a:\fuelcon.dat” FOR OUTPUT AS #1 CLS PRINT “enter comma to quit” INPUT “style and fuel”; style, fuel DO WHILE style <> ““ WRITE #1, style, fuel INPUT “style and fuel”; style, fuel LOOP CLOSE #1

12 Task u 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 <> ““

13 checking the file’s contents u use Qbasic editor to open the file –FILE, OPEN, PATH, name & extension –NB qbasic automatically looks for *.BAS files, modify if necessary u write a program to look at the file

14 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

15 Reading DATA from a File OPEN “path/filename” FOR INPUT AS #filenumber INPUT #filenumber, variablelist u is used to read data (in) from a file: e.g. INPUT #2, stockname$, numheld, price u reads from file number 2 the next three fields and assigns them to stocknames$, numheld, price.

16 Detecting the End of the File u The EOF function: u 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 u e.g. EOF(2) u when last record of file number 2 is read EOF is set TRUE u use with DO LOOPs

17 Reading Data from the file OPEN file WHILE not EOF get record with INPUT Process record LOOP CLOSE file

18 Fuel Consumtion Problem DIM make AS STRING DIM fuel AS SINGLE OPEN “a:\fuelcon.dat” FOR INPUT AS #1 CLS PRINT “...”‘headings DO WHILE NOT EOF(1) INPUT #1 make, fuel PRINT make, fuel LOOP CLOSE #1

19 Task u rewite program using pretest DO UNTIL PRINT “...”‘headings DO WHILE NOT EOF(1) INPUT #1 make, fuel PRINT make, fuel LOOP CLOSE #1

20 Summary of OPEN u The OPEN statement: OPEN “path/filename” FOR mode AS #filenumber u path - the location of the file –drive\directory\sub directory\...\filename –c:\e0001\data.bas –a:\work\logger1\temp.*

21 MODE - output, input, append u 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. u 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. u APPEND - The file exists and records will be added to the end of it. If the file specified does not exist, QB creates it.

22 The WRITE statement: u puts the data in the file: WRITE #filenumber, expressionlist u #filenumber is the filenumber specified in a previous OPEN statement u expressionlist is the values to be written to the file e.g. WRITE #2, Stocknames$, numheld, price

23 The CLOSE statement: CLOSE [#filenumber], [#filenumber] u tells QB finished with file u ends association between filename and filenumber e.g. CLOSE #1,#3

24 The INPUT statement (for files): u is used to read data (in) from a file: INPUT #filenumber, variablelist e.g. INPUT #2, stockname$, numheld, price u reads from file number 2 the next three fields and assigns them to stocknames$, numheld, price.

25 The INPUT$ statement: u returns a string of characters read from a specified file: INPUT$ ( n (, [#] filenumber% ] ) u n: is the number of characters to read u #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


Download ppt "Files Organisation sequential files. Readings u Schneider Chapter 8 u Shelly Cashman 1997 9.2 to 9.14; 1995 9.4 to 9.11 u Meyer 1997 2-29 to 2-37; 1995."

Similar presentations


Ads by Google