Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

derived type binary io

eide
Beginner
694 Views
Hey,

please see the following lines.

[fortran]	program dttest
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
!	check derived type structure
!	compile -> ifort dttest.f90 -o xdttest
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	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 dttest[/fortran]
The program compiles and runs without a problem.
This is what I get as output 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.059 on a suse 11.2 64bit
machine and I hope one of you can help me with an idea how to use
the derived type structure and read the values correctly.
Thanks in advance.

greets

Eide
0 Kudos
2 Replies
Hirchert__Kurt_W
New Contributor II
694 Views
I believe Steve posted in another thread that CONVERT='BIG_ENDIAN' is not applied to user-defined types, only intrinsic types.
-Kurt
0 Kudos
eide
Beginner
694 Views
Hey Kurt,

I found the thread you mentioned. Seems like there was no change to it since 2008. The possibilty to use derived type structures would really simplify and speed up the file reading.

Eide
0 Kudos
Reply