program example
  use csvfile_module
  type (csvfile_t) x
  integer a, n
  integer (1) b
  integer (2) c
  integer (selected_int_kind(18)) d, e(10)
  real y
  double precision z
  character (:), allocatable :: string
  logical eof, eor

  call x%open('data1.csv',status=csvopen_old)

! We expect at least four integer values in the first record.
! (Error will be raised otherwise.)

  call x%read(a)
  call x%read(b)
  call x%read(c)
  call x%read(d)
  if (x%at_eor()) then
    print *, 'Exactly 4 values in the first record'
  else
    print *, 'More than 4 values in the first record'
  end if
  print *, 'Read', a, b, c, d

! We expect any number of integer values in the second record,
! display up to SIZE(E) of them.

  call x%next_record
  do i = 1, size(e)
    call x%read(e(i),eor=eor)
    if (eor) exit
  end do
  i = i - 1
  if (eor) then
    print *, 'Exactly', i, 'values in the second record:', e(:i)
  else
    print *, 'More than', i, 'values in the second record', e(:i)
  end if

! We expect two REAL values in the third record.

  call x%next_record
  call x%read(y)
  call x%read(z)
  print *, 'Read', y, z
  if (x%at_eor()) then
    print *, 'Exactly 2 values in the third record'
  else
    print *, 'More than 2 values in the third record'
  end if

! We expect one integer and one character value in the fourth record.

  call x%next_record
  call x%read(a)
  call x%read(string)
  print *, a, string

! Count how many more records are left.

  call x%next_record ! Skip the rest of the current record first.
  do
    call x%next_record(eof=eof)
    print *, "Next Record"
    if (eof) exit
    do
      if ( .not. x%at_eor()) then
        print *, 'Not EOR so now about to call read()...'
        call x%read(b)
        print *, "'", b, "'"
      else
        print *, 'EOR found...'
        exit
      end if
    end do
  end do

! Finished - close the CSV file.

  call x%close
  print *, 'ok'
end program example
