C Collection of code snippets by Arne Vajhøj 
C (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) 
      program rel
      implicit none
      write(*,*) 'Fortran'
      call test_write()
      call test_read_sequential()
      call test_read_random()
      end
c
      subroutine test_write()
      implicit none
      open(unit=1, file='for.rel', status='new', form='formatted',
     +     organization='relative', recl=504, recordtype='variable',
     +     access='direct')
      call test_write_one(3, 'CCC')
      call test_write_one(2, 'BB')
      call test_write_one(1, 'A')
      close(unit=1)
      end
c
      subroutine test_write_one(ix, val)
      implicit none
      integer*4 ix
      character*(*) val
      write(unit=1, rec=ix) val
      end
c
      subroutine test_read_sequential()
      implicit none
      integer*4 buflen
      character*80 buf
      open(unit=1, file='for.rel', status='old', form='formatted',
     +     organization='relative', recl=504, recordtype='variable',
     +     access='sequential')
100   read(unit=1, fmt='(q,a)', end=200) buflen,buf
      write(*,*) '|'//buf(1:buflen)//'|', buflen
      goto 100
200   close(unit=1)
      end
c
      subroutine test_read_random()
      implicit none
      open(unit=1, file='for.rel', status='old', form='formatted',
     +     organization='relative', recl=504, recordtype='variable',
     +     access='direct')
      call test_read_random_one(3)
      call test_read_random_one(2)
      call test_read_random_one(1)
      close(unit=1)
      end
c
      subroutine test_read_random_one(ix)
      implicit none
      integer*4 ix
      integer*4 buflen
      character*80 buf
      read(unit=1, fmt='(q,a)', rec=ix) buflen,buf
      write(*,*) '|'//buf(1:buflen)//'|', buflen
      end
