PROGRAM Merging_Files
!-----------------------------------------------------------------------
! Program to read two files of records containing a student number, a
! student name, and a cumulative GPA, where the files have been sorted 
! so that student numbers are in ascending order, and merge these two
! files to produce another that is also sorted.  Variables used are:
!     File          : array of 3 file names
!     OpenStatus    : array of 3 status variables for OPEN
!     InputStatus   : array of 2 status variables for READ
!     StudentName   : array of 2 student names
!     StudentNumber : array of 2 student numbers
!     GPA           : array of 2 cumulative GPAs
!     I             : subscript
!
! Input (keyboard): File
! Output (screen):  User prompts and message that sorting has been
!                   completed
! Input (files):    Student records from File(1) and File(2)
! Output (file):    Student records to File(3)
!-----------------------------------------------------------------------

  IMPLICIT NONE
  CHARACTER(20), DIMENSION(3) :: File
  INTEGER, DIMENSION(3) :: OpenStatus
  CHARACTER(20), DIMENSION(2) :: StudentName
  INTEGER, DIMENSION(2) :: InputStatus, StudentNumber
  REAL,  DIMENSION(2) :: GPA
  INTEGER :: I

  ! Get the names of the files and open them

  PRINT *, "Enter the names of the files to be merged and the name"
  PRINT *, "of the file to be produced:"
  READ *, File
  DO I = 1, 2
     OPEN (UNIT = 10*I, FILE = File(I), STATUS = "OLD", &
           ACTION = "READ", POSITION = "REWIND", &
           ACCESS = "SEQUENTIAL", IOSTAT = OpenStatus(I))
  END DO

  OPEN (UNIT = 30, FILE = File(3), STATUS = "NEW", &
        ACTION = "WRITE", ACCESS = "SEQUENTIAL", IOSTAT = OpenStatus(3))

  DO I = 1, 3
     IF (OpenStatus(I) > 0) THEN
        PRINT *, "*** File #", I, "cannot be opened *** "
        STOP
     END IF
  END DO

  ! Read first record from each file
  DO I = 1, 2
     READ (UNIT = 10*I, FMT = 100, IOSTAT = InputStatus(I)) &
           StudentNumber(I), StudentName(I), GPA(I)
     IF (InputStatus(I) > 0) STOP "*** Input error ***"
  END DO
  100 FORMAT(I5, 1X, A, F4.2)

  ! While neither the end of File(1) or File(2) has been reached,
  ! do the following:
  DO
     IF (InputStatus(1) < 0 .OR. InputStatus(2) < 0) EXIT
     ! If at the end of either file, terminate repetition

     ! Otherwise continue with the following
     IF (StudentNumber(1) < StudentNumber(2)) THEN
        I = 1
     ELSE
        I = 2
     END IF

     WRITE (UNIT = 30, FMT = 100) StudentNumber(I), StudentName(I), GPA(I)
     READ (UNIT = 10*I, FMT = 100, IOSTAT = InputStatus(I)) &
              StudentNumber(I), StudentName(I), GPA(I)
  END DO

  ! If more records remain in either file, copy them to File_3

  DO I = 1, 2
    DO
       IF (InputStatus(I) < 0) EXIT
       WRITE (UNIT = 30, FMT = 100) StudentNumber(I), StudentName(I), GPA(I)
       READ (UNIT = 10*I, FMT = 100, IOSTAT = InputStatus(I)) &
              StudentNumber(I), StudentName(I), GPA(I)
    END DO
  END DO

  PRINT *
  PRINT *, "File merging is complete"

END PROGRAM Merging_Files
