1 / 25

Files Organisation

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

Download Presentation

Files Organisation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Files Organisation sequential files

  2. 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

  3. 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

  4. Q12345678 john smith pass <e> field #1 field #2 field #3 field terminator (optional) • 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

  5. 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

  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 • 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

  8. 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

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

  10. write the record get new data Example building a file start create an empty file get a record WHILE valid data to enter 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 • 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 • 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

  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 • 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.

  16. 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

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

  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 • 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 • 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.*

  21. 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.

  22. 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

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

  24. 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.

  25. 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

More Related