1 / 25

File Handling

File Handling. Indexed Sequential Files. File Organisation and Access. File Organisation How is the file organised ? Within program and on storage media File Access How will you access the file ?. File Organisation. Indexed Sequential Have two parts Index File Body [Data]

kaliska
Télécharger la présentation

File Handling

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. File Handling Indexed Sequential Files

  2. File Organisation and Access • File Organisation • How is the file organised ? • Within program and on storage media • File Access • How will you access the file ?

  3. File Organisation • Indexed Sequential • Have two parts • Index • File Body [Data] • Each record has a key but the Index defines the location of the record in the file • Records are stored in the order in which they are written but retrieved using the index Index Position Key 1 6 Key 2 3 Key 4 - Key 5 2 Key10 Key5 Key2 Key6 Key3 Key1 ….. Keyn

  4. File Access • Method used to allow reading from/writing to a file • 3 types of access • Sequential • Random • Dynamic

  5. File Access • Sequential Access • Next record in sequence is read/written • A file whose organisation is sequential can only have sequential access • Random Access • Record is read/written by specifying its KEY • Relative or Indexed Sequential Files • Dynamic Access • Combination of the above • Relative or Indexed Sequential files

  6. Declare the file to the program Configuration Section, Input-Output-Section, File-Control Data Division, File Section Open file for reading/writing If reading read record and check for end of file if reading all If writing move relevant details to file record and then write Close file Recap Using Sequential Files

  7. File Management System looks after indexing Records can be accessed directly or sequentially Each record has one or more key fields Overuse of indexing is inefficient – leads to inefficiency Good in multi-user environment Indexed Sequential Files

  8. Cobol - Indexed Sequential files • Files where records can be identified by the value in one of the fields • student number in a student file • phone number in a phone file • claim number in an insurance file • account number in a bank file

  9. Indexed Sequential Files • Suitable For • Medium to large amount of data • Data filed mainly by one unique key • Regular direct/sequential access is required • Low volatility or growth • Useful for interactive processing

  10. Using Indexed Sequential Files • Declare the Files to the program • Need to indicate what the organisation is • Need to declare the keys • Within the select statement for the file • Creating the file • Must write the record using the key declared

  11. Selecting the file • Select the file giving • Organization is indexed • Access mode is • Sequential or • Random or • Dynamic • Record key is name of key field of record definition • Possible multiple alternate record keys

  12. Access mode • Sequential • Accessed in sequential order of the key field value • Random • Determined by setting value of the key field • Dynamic • Combination of random and sequential

  13. Selection statement Select STUDENT-FILE assign “C:\STUDENT.DAT" Organization is Indexed Access Mode is Dynamic Record Key is STUDENT-ID Alternate Record Key is STUDENT-CLASS with duplicates.

  14. Defining the record • The record is defined using the file descriptor as before • All record keys are included as fields in the record FD STUDENT-FILE. 01 STUDENT-RECORD. 02 STUDENT-ID PIC 9(7). 02 STUDENT-NAME PIC X(20). 02 STUDENT-CLASS PIC X(6). 02 STUDENT-YEAR PIC 9.

  15. File operations • The file can be opened for • INPUT • OUTPUT • EXTEND • INPUT-OUTPUT • The file is closed as before. • Open I-O STUDENT-FILE. • Close STUDENT-file.

  16. Reading from the file • To do a direct read • put a value into the key field and then execute a read statement. READ FILE-NAME INVALID KEY INVALID KEY CLAUSE {NOT INVALID KEY NOT INVALID KEY CLAUSE} END-READ • Invalid Key Clause • invoked when there is no record there which matches the key value supplied. • This is a failure. • End of file is considered a failure.

  17. Direct (keyed) read. Move 1234567 to STUDENT-ID. Read Student-file Record Invalid Key Display "Student Does Not Exist" at 2330 Not Invalid Key Set Student-found to True End-read.

  18. Reading Sequentially from the file • Need to position pointer in the file • START verb is used to give the record key the starting value START FILE-NAME KEY NOT < KEY-NAME INVALID KEY INVALID KEY CLAUSE {NOT INVALID KEY NOT INVALID KEY CLAUSE} END-START • Read statement is as for sequential files • Need additional control construct to cope with failure to match patterns

  19. Starting the read Move start-student-class to student-class. Start student-file key not < student-class invalid key set eof to true not invalid key set not-eof to true read student-file next record at end set eof to true end-read end-start

  20. Continuing the read (display only class DT266/2) Perform until eof or student-class NOT = “DT266/2” Perform Display-Student-Batch-Details Read student-file next record At end set eof to true End-read End-perform

  21. Writing to the file • A RECORD is written, once it is filled. • It derives it’s identity from the value in the key field. • If this is a duplicate, an ‘invalid key’ condition results and the write fails WRITE STUDENT-RECORD INVALID KEY DISPLAY "CODE ALREADY IN USE" AT 2330 NOT INVALID KEY DISPLAY "WRITING ..." AT 2330 END-WRITE.

  22. REWRITE Format • A record is rewritten, once it is filled. It derives it’s identity from the value in the key field. • If the record is not already there, an invalid key condition results - a failure. REWRITE RECORD-NAME INVALID KEY INVALID KEY CLAUSE {NOT INVALID KEY NOT INVALID KEY CLAUSE} END-REWRITE {} indicate optional clause

  23. Rewrite REWRITE STUDENT-RECORD INVALID KEY DISPLAY "THE STUDENT WITH THIS CODE NO LONGER EXISTS" AT 2330 NOT INVALID KEY DISPLAY "UPDATING ..." AT 2330 END-REWRITE.

  24. DELETING RECORDS • Before deleting, always ask the user to confirm. DELETE FILE-NAME RECORD(S) INVALID KEY INVALID KEY CLAUSE {NOT INVALID KEY NOT INVALID KEY CLAUSE} END-DELETE.

  25. Deleting DISPLAY "ARE YOU SURE YOU WISH TO DELETE THIS RECORD?[Y TO DELETE]" AT 2110 ACCEPT CONFIRM-DELETE LINE 21 COLUMN 70 IF DELETE-CONFIRMED DELETE STUDENT-FILE RECORD INVALID KEY DISPLAY "RECORD HAS BEEN DELETED PRIOR TO THIS" AT 2210 NOT INVALID KEY DISPLAY "DELETING RECORD" LINE 22, COLUMN 10 END-DELETE

More Related