PROGRAM Make_Direct_Access_Inv_File
! Program to create direct-access inventory file (fil12-4a.dat)
! from sequential-access inventory file (fil12-4s.dat)

  TYPE InventoryRecord
    INTEGER :: ItemNumber
    CHARACTER(25) :: ItemName
    INTEGER :: LotSize
    REAL :: Price
    INTEGER :: ReorderPoint, &
               NumberInStock, &
               OptimalInvLevel
  END TYPE InventoryRecord

  TYPE(InventoryRecord) :: InvRecord
  CHARACTER(20) :: InputFile, OutputFile
  INTEGER :: EndOfFile, RecordLength, I

  WRITE (*, '(1X, A)', ADVANCE = "NO") &
         "Enter name of sequential inventory file: "
  READ *, InputFile
  WRITE (*, '(1X, A)', ADVANCE = "NO") &
         "Enter name of new direct-access inventory file to create: "
  READ *, OutputFile
  WRITE (*, '(1X, A)', ADVANCE = "NO") &
         "Enter length of records for new file: "
  READ *, RecordLength
  OPEN(UNIT = 9, FILE = InputFile, STATUS = "OLD", &
       FORM = "FORMATTED", POSITION = "REWIND", &
       ACTION = "READ",  ACCESS = "SEQUENTIAL")

  OPEN(UNIT = 10, FILE = OutputFile, STATUS = "NEW", &
       FORM = "FORMATTED", ACTION = "WRITE", &
       ACCESS = "DIRECT", RECL = RecordLength)

  DO I = 1, 10000
    READ (UNIT = 9, FMT = 10, IOSTAT = EndOfFile) &
         InvRecord%ItemNumber, InvRecord%ItemName, InvRecord%LotSize, &
         InvRecord%Price, InvRecord%ReorderPoint, &
         InvRecord%NumberInStock, InvRecord%OptimalInvLevel
    10 FORMAT(I4, A, I4, F6.2, 3I6)
    IF (EndOfFile < 0) EXIT

    WRITE(UNIT = 10, FMT = 10, REC = I) &
         InvRecord%ItemNumber, InvRecord%ItemName, InvRecord%LotSize, &
         InvRecord%Price, InvRecord%ReorderPoint, &
         InvRecord%NumberInStock, InvRecord%OptimalInvLevel

  END DO

END PROGRAM Make_Direct_Access_Inv_File


