Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Derived type binary io 1

Status
Not open for further replies.

Eide

Technical User
Apr 22, 2010
7
NO
! Hei Hei
!
! please see the following lines
!
program tektip
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! check derived type structure for binary io
! compile -> ifort tektip.f90 -o xtektip
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
implicit none
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
integer (kind = 2) :: aone,atwo
integer (kind = 4) :: athree,afour,afive
real (kind = 4) :: asix
integer (kind = 2) :: bone,btwo
integer (kind = 4) :: bthree,bfour,bfive
real (kind = 4) :: bsix
! type construct
type teststruct
integer (kind = 2) :: cone
integer (kind = 2) :: ctwo
integer (kind = 4) :: cthree
integer (kind = 4) :: cfour
integer (kind = 4) :: cfive
real (kind = 4) :: csix
end type teststruct
!
type(teststruct) :: bintest
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! initial values
aone = 2
atwo = 4
athree = 1024
afour = 161257
afive = 752161
asix = 12.3456
!
open(convert = 'BIG_ENDIAN', unit = 10, file = 'test.bin', form = 'binary')
! write testdata
write(10)aone,atwo,athree,afour,afive,asix
write(10)aone,atwo,athree,afour,afive,asix
write(10)aone,atwo,athree,afour,afive,asix
write(10)aone,atwo,athree,afour,afive,asix
close(10)
!
open(convert = 'BIG_ENDIAN', unit = 11, file = 'test.bin', form = 'binary')
! read testdata
read(11)bone,btwo,bthree,bfour,bfive,bsix
print*,bone,btwo,bthree,bfour,bfive,bsix
read(11)bone,btwo,bthree,bfour,bfive,bsix
print*,bone,btwo,bthree,bfour,bfive,bsix
read(11)bone,btwo,bthree,bfour,bfive,bsix
print*,bone,btwo,bthree,bfour,bfive,bsix
read(11)bone,btwo,bthree,bfour,bfive,bsix
print*,bone,btwo,bthree,bfour,bfive,bsix
close(11)
!
open(convert = 'BIG_ENDIAN', unit = 12, file = 'test.bin', form = 'binary')
! read testdata
read(12)bintest
print*,bintest
read(12)bintest
print*,bintest
read(12)bintest
print*,bintest
read(12)bintest
print*,bintest
close(12)

end program tektip
!
! this is the what I get on my screen
!
! 2 4 1024 161257 752161 12.34560
! 2 4 1024 161257 752161 12.34560
! 2 4 1024 161257 752161 12.34560
! 2 4 1024 161257 752161 12.34560
! 512 1024 262144 -378207744 561646336 -1.3658832E-26
! 512 1024 262144 -378207744 561646336 -1.3658832E-26
! 512 1024 262144 -378207744 561646336 -1.3658832E-26
! 512 1024 262144 -378207744 561646336 -1.3658832E-26
!
! Why is the output not the same? I stumbled over this in an other
! program and wrote these lines just for demonstration. I really
! would like to use derived type structures as the real data files I
! want to read have various repeating integer headers and no fixed
! length data records with real numbers. I tried the same without
! convert = 'BIG_ENDIAN' and it works fine but the data I have
! to read is big endian.
! I am using the intel fortran compiler 11.1 on a linux machine and
! I hope one of you can help me. Thanks in advance.
!
! Hilsen, from Norway
 
Convert and form='binary' must be a new thing on 11.1. It doesn't work on my version 7 compiler.
 
Looks like an alignment problem. The problem is you can't compact the structures. It looks like there is a compiler bug where structures read in the default format. What you could do is read the elements individually. Try the following - you'll see what I mean.
Code:
! 
!    please see the following lines
!
    program tektip
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
!    check derived type structure for binary io
!    compile -> ifort tektip.f90 -o xtektip
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    implicit none
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    integer (kind = 2) :: aone,atwo
    integer (kind = 4) :: athree,afour,afive
    real    (kind = 4) :: asix
    integer (kind = 2) :: bone,btwo
    integer (kind = 4) :: bthree,bfour,bfive
    real    (kind = 4) :: bsix
!    type construct
    type teststruct
      integer (kind = 2) :: cone
      integer (kind = 2) :: ctwo
      integer (kind = 4) :: cthree
      integer (kind = 4) :: cfour
      integer (kind = 4) :: cfive
      real    (kind = 4) :: csix
    end type teststruct
!
    type(teststruct) :: bintest
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!    initial values
    aone = 2
    atwo = 4
    athree = 1024
    afour = 161257
    afive = 752161
    asix = 12.3456
!
    open(unit = 10, file = 'test.bin', form = 'unformatted', convert='BIG_ENDIAN')
!    write testdata
    write(10)aone,atwo,athree,afour,afive,asix
    write(10)aone,atwo,athree,afour,afive,asix
    write(10)aone,atwo,athree,afour,afive,asix
    write(10)aone,atwo,athree,afour,afive,asix
    close(10)
!
    open(unit = 11, file = 'test.bin', form = 'unformatted', convert='BIG_ENDIAN')
!    read testdata
    read(11)bone,btwo,bthree,bfour,bfive,bsix
    print*,bone,btwo,bthree,bfour,bfive,bsix
    print '(2(1X,Z4), 4(1X,Z8))', bone, btwo, bthree, bfour, bfive, bsix
    read(11)bone,btwo,bthree,bfour,bfive,bsix
    print*,bone,btwo,bthree,bfour,bfive,bsix
    read(11)bone,btwo,bthree,bfour,bfive,bsix
    print*,bone,btwo,bthree,bfour,bfive,bsix
    read(11)bone,btwo,bthree,bfour,bfive,bsix
    print*,bone,btwo,bthree,bfour,bfive,bsix
    close(11)
!
    open(unit = 12, file = 'test.bin', form = 'unformatted', convert='BIG_ENDIAN')
!    read testdata
    read(12)bintest
    print*,bintest
    print '(2(1X,Z4), 4(1X,Z8))', bintest
    read(12)bintest
    print*,bintest
    read(12)bintest
    print*,bintest
    read(12)bintest
    print*,bintest
    close(12)
    open(unit = 12, file = 'test.bin', form = 'unformatted', convert='BIG_ENDIAN')
!    read testdata
    read(12)bintest%cone,bintest%ctwo,bintest%cthree,bintest%cfour,bintest%cfive,bintest%csix
    print*,bintest
    read(12)bintest%cone,bintest%ctwo,bintest%cthree,bintest%cfour,bintest%cfive,bintest%csix
    print*,bintest
    read(12)bintest%cone,bintest%ctwo,bintest%cthree,bintest%cfour,bintest%cfive,bintest%csix
    print*,bintest
    read(12)bintest%cone,bintest%ctwo,bintest%cthree,bintest%cfour,bintest%cfive,bintest%csix
    print*,bintest
    close(12)
    stop
    end program tektip
!
 
Figured out what it is: if the item read is not 2, 4 or 8 bytes, it assumes no endianess and reads the item as-is.
 
Hi xwb,

thanks for the help. I think I will use the posibility to read the elements of the type structure individually. I do not get the improved runtime for reading but I can use the structure later.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top