Download Sequential Files - Fortran Programming - Lecture Notes and more Study notes Computer Programming in PDF only on Docsity!
Sequential Files
Files and records: • A file is collection of data located on a secondary storage
• device.The data in a file are organized into components called
• records.Thus, a record is a set of data items, while a file is a set of
records.
Sequential Files and Serial Access: • In a sequential file, the records must be accessed in order
• starting with the first record; that is serially.Each record in the file may only be accessed immediately
after accessing the previous record in the file.
Direct Access Files and Random Access: • In a direct access file, the records may be accessed in any
• order; that is, randomly.All records in a direct access file must be the same length.
Unit Number • Within a FORTRAN program, each active file is referred:
to by using a unit number, a non-negative integer.
OPEN Statement: • The OPEN statement is used to establish an association
• between a particular file and a particular unit number.It connects the file and the unit number.
OPEN (UNIT = unit-number, FILE = file-name... )
record length must be provided. is an integer. len
Special sequential operations.
Two routines allows special operations on sequential files:
The BACKSPACE Statement • The BACKSPACE statement is used to move from the:
current record to the previous record in the sequential fileassociated with the specified unit number.
BACKSPACE (UNIT = unit-number)
The REWIND Statement • The REWIND statement is used to position to the first:
record in the sequential file associated with the specifiedunit number.
REWIND (UNIT = unit-number)
Example: Extending a file.
C Reads the data for each book.SUBROUTINE GETREC (STOCK, AUTHOR, TITLE, PRICE, QUANT) C Declarations. INTEGER STOCK, QUANT REAL PRICE
C Enter the book data.^ CHARACTER * 20 AUTHOR, TITLE PRINT *, 'Stock number (enter 0 when done): ' READ *, STOCK IF (STOCK .NE. 0) THEN PRINT *, 'Author in quotes: ' READ *, AUTHOR PRINT *, 'Title in quotes: ' READ *, TITLE PRINT *, 'Price: ' READ *, PRICE ENDIF^ PRINT *, 'Quantity: '^ READ *, QUANT C Exit subroutine. RETURN END
Direct Access Files
Direct Access Files and Random Access: • Records in a random access file are all of the same length
- Each record has a unique number associated with itbeginning with one (1) for the first record.
- These records may be accessed in an arbitrary or randomorder.
The OPEN Statement • The OPEN statement is used to specify the length of the:
records in a direct access file. This length is measured incharacters.
OPEN ( UNIT = unit-number, FILE = file-name,
ACCESS = 'DIRECT', RECL= record-length,
FORM =.. .)
- Records in a direct access file may be either'FORMATTED' or 'UNFORMATTED'.
- If this parameter (FORM = ) is omitted,'UNFORMATTED' will be assumed for direct access and
'FORMATTED' for sequential access.
Random Access Files: • There are special forms of the WRITE and READ
- statements for randomly accessing a direct access file.The number of the record to be accessed must be specified.
Initializing A Direct Access File: • It is good programming practice to initialize every record
in a direct access file to some unique value, perhaps allblanks.
- This enables the program to determine later if a record"exists" or not when the file is READ.
Common Programming Errors
- When working with files, be sure to use an OPENstatement. Be sure to use the proper format when reading a
- file.When reading a sequential file, remember to use the END
= label option. When writing a sequential file, remember towrite the end-of-file record.
- When working with direct access files, be sure to use thecorrect record number in the REC = option.
Example: • CREATE - Create the file A Inventory File System using direct access
- • INTLIZ - Write blanks to all recordsGETREC - defined earlier, gets record data from user.
- UPDATE - Updates data in inventory file
C Write the book store inventory to a direct access file. C The file data are read from the keyboard.PROGRAM CREATE C Declarations. INTEGER SENVAL, MAXSTK PARAMETER (SENVAL = 0, MAXSTK = 1000) INTEGER STOCK, QUANT, TOTAL REAL PRICE CHARACTER * 20 AUTHOR, TITLE
C Open file INVEN for output.^ CHARACTER * 8 DATE +^ OPEN (UNIT = 2, FILE = 'DIRINV', FORM = 'FORMATTED', ACCESS = 'DIRECT', RECL = 54, STATUS = 'NEW') C Get date, initialize TOTAL, and initialize file DIRINV C to blanks. PRINT *,'Enter the date in the form ''MM/DD/YY'' : ' READ *, DATE PRINT *, 'Enter TOTAL = 0 each book record as of ', DATE
C Read each book record, write it to file DIRINV,^ CALL INTLIZ (2) C and accumulate the total number of books in TOTAL. CALL GETREC (STOCK, AUTHOR, TITLE, PRICE, QUANT) DO WHILE ((STOCK .GT. SENVAL) .AND.
- (^) WRITE (UNIT = 2, FMT = 21, REC = STOCK)STOCK, AUTHOR, TITLE, PRICE, QUANT(STOCK .LE. MAXSTK)) 21 FORMAT (I4, A20, A20, F6.2, I4) TOTAL = TOTAL + QUANT CALL GETREC (STOCK, AUTHOR, TITLE, PRICE, QUANT) ENDDO
C Update a direct access inventory file. C Reads the record number and quantity orderedPROGRAM UPDATE C from the keyboard. C for each record selected. Updates the inventory amount C Declarations. INTEGER SENVAL, MAXSTK PARAMETER (SENVAL = 0, MAXSTK = 1000) INTEGER STOCK, QUANT, TOTAL, RECNUM REAL PRICE CHARACTER *20 AUTHOR, TITLE
C Open file INVEN for update.^ CHARACTER *8 DATE
- +^ OPEN (UNIT = 2, FILE = 'DIRINV', FORM = 'FORMATTED', ACCESS = 'DIRECT',RECL = 54, STATUS = 'OLD') C Read the record number and quantity of C each book sold. C Update the inventory record if order is valid. +^ PRINT *, 'Enter record number of book sold', (^) READ *, RECNUM' (or 0 to stop):' +^ DO WHILE ((RECNUM .GT. SENVAL) .AND. (^) PRINT *, 'Enter quantity sold: '(RECNUM .LE. MAXSTK)) C READ *, ORDERGet record RECNUM.
- READ (UNIT = 2, FMT = 21, REC = RECNUM)STOCK, AUTHOR, TITLE, PRICE, QUANT C (^) + If order is valid, update the inventory quantity. IF ((STOCK .EQ. RECNUM) .AND.(QUANT .GE. ORDER)) THEN QUANT = QUANT - PRINT *, 'New inventory amount = ', QUANT WRITE (UNIT = 2, FMT = 21, REC = RECNUM) ORDER 21 + (^) ELSE IF (STOCK .EQ. 0) THENFORMAT (I4, A20, A20, F6.2, I4)STOCK, AUTHOR, TITLE, PRICE, QUANT
ELSE IF (STOCK .NE. RECNUM) THENPRINT *, 'Error - PRINT *,^ record does not exist' +'Error - record and stock number don''t match' ELSE PRINT *, +'Inventory insufficient to fill order' ENDIF
- PRINT *, 'Enter record number of book sold', READ *, RECNUM'( or 0 to stop):' ENDDO IF (RECNUM .GT. MAXSTK)
- (^) CLOSE (UNIT = 2)PRINT *, 'Record number out-of-range' C Exit program. STOP END