Code Comments
Programming Forum and web based access to our favorite programming groups.Hello, I'm trying to port a data saving routine from a FORTRAN 77 program to Fortran 95 but stuck with the following read problem : FORTRAN 77 'unform_types.f': program main implicit none integer :: i = 7, j, k open(unit=12,file='Itype77.dat',status='replace', % form='unformatted') write(12),i close(12) open(unit=12,file='Itype95.dat',form='unformatted') read(12),j close(12) write(*,*) 'Itype95', j open(unit=12,file='Itype77.dat',form='unformatted') read(12),k close(12) write(*,*) 'Itype77', k end program main the "ported" Fortran 95 Code 'unform_types.f95' : program main implicit none integer(kind=4) :: i = 7, j, k open(unit=12,file='Itype95.dat',status='replace', & form='unformatted') write(12),i close(12) open(unit=12,file='Itype95.dat',form='unformatted') read(12),j close(12) write(*,*) 'IType95', j open(unit=12,file='Itype77.dat',form='unformatted') read(12),k close(12) write(*,*) 'IType77', k end program main *Compiling with: philscher@Ariel:~/Science/HiWi - MPIA/Hydrocode/FortranBox$ ls -l Itype* -rw-r--r-- 1 philscher philscher 12 2007-08-20 19:17 Itype77.dat -rw-r--r-- 1 philscher philscher 20 2007-08-20 19:17 Itype95.dat philscher@Ariel:~/Science/HiWi - MPIA/Hydrocode/FortranBox$ f95 -o for95 unform_types.f95 & f77 -o for77 unform_types.f *and running, produces following results on my machine philscher@Ariel:~/Science/HiWi - MPIA/Hydrocode/FortranBox$ ./for77 Itype95 0 Itype77 7 philscher@Ariel:~/Science/HiWi - MPIA/Hydrocode/FortranBox$ *and philscher@Ariel:~/Science/HiWi - MPIA/Hydrocode/FortranBox$ ./for95 IType95 7 IType77 4 philscher@Ariel:~/Science/HiWi - MPIA/Hydrocode/FortranBox$ philscher@Ariel:~/Science/HiWi - MPIA/Hydrocode/FortranBox$ ls -l Itype* -rw-r--r-- 1 philscher philscher 12 2007-08-20 19:17 Itype77.dat -rw-r--r-- 1 philscher philscher 20 2007-08-20 19:17 Itype95.dat philscher@Ariel:~/Science/HiWi - MPIA/Hydrocode/FortranBox$ *Why are the results different ? The programs produces Itype*.dat files that are different in size. I played around with the integer(kind=...) parameter in the F-95 Code but never get the desired result. I think the problem lies that Fortran-95 uses a 8-byte long record header, while F-77 uses only 4-byte. But why is this so and what should I do to get things work ? Thanks for reading & helping Paul
Post Follow-up to this messageOn 2007-08-20 14:26:18 -0300, Paul Hilscher <p.hilscher@gmx.net> said: > Hello, > > I'm trying to port a data saving routine from a FORTRAN 77 program to > Fortran 95 but stuck with the following read problem : > > FORTRAN 77 'unform_types.f': > > program main > implicit none > integer :: i = 7, j, k > open(unit=12,file='Itype77.dat',status='replace', > % form='unformatted') > write(12),i > close(12) > > open(unit=12,file='Itype95.dat',form='unformatted') > read(12),j > close(12) > write(*,*) 'Itype95', j > > open(unit=12,file='Itype77.dat',form='unformatted') > read(12),k > close(12) > write(*,*) 'Itype77', k > > > end program main > > > the "ported" Fortran 95 Code 'unform_types.f95' : > > program main > implicit none > integer(kind=4) :: i = 7, j, k > open(unit=12,file='Itype95.dat',status='replace', & > form='unformatted') > write(12),i > close(12) > > open(unit=12,file='Itype95.dat',form='unformatted') > read(12),j > close(12) > write(*,*) 'IType95', j > > open(unit=12,file='Itype77.dat',form='unformatted') > read(12),k > close(12) > write(*,*) 'IType77', k > > end program main > > > *Compiling with: > philscher@Ariel:~/Science/HiWi - MPIA/Hydrocode/FortranBox$ ls -l Itype* > -rw-r--r-- 1 philscher philscher 12 2007-08-20 19:17 Itype77.dat > -rw-r--r-- 1 philscher philscher 20 2007-08-20 19:17 Itype95.dat > philscher@Ariel:~/Science/HiWi - MPIA/Hydrocode/FortranBox$ > > f95 -o for95 unform_types.f95 & > f77 -o for77 unform_types.f > > *and running, produces following results on my machine > > philscher@Ariel:~/Science/HiWi - MPIA/Hydrocode/FortranBox$ ./for77 > Itype95 0 > Itype77 7 > philscher@Ariel:~/Science/HiWi - MPIA/Hydrocode/FortranBox$ > > *and > > philscher@Ariel:~/Science/HiWi - MPIA/Hydrocode/FortranBox$ ./for95 > IType95 7 > IType77 4 > philscher@Ariel:~/Science/HiWi - MPIA/Hydrocode/FortranBox$ > > philscher@Ariel:~/Science/HiWi - MPIA/Hydrocode/FortranBox$ ls -l Itype* > -rw-r--r-- 1 philscher philscher 12 2007-08-20 19:17 Itype77.dat > -rw-r--r-- 1 philscher philscher 20 2007-08-20 19:17 Itype95.dat > philscher@Ariel:~/Science/HiWi - MPIA/Hydrocode/FortranBox$ > > *Why are the results different ? The programs produces Itype*.dat files > that are different in size. I played around with the integer(kind=...) > parameter in the F-95 Code but never get the desired result. I think the > problem lies that Fortran-95 uses a 8-byte long record header, while > F-77 uses only 4-byte. But why is this so and what should I do to get > things work ? Probably. Why does it matter? Unformatted i/o is intended to be read in by the same program (or at least one of its close relatives). Reading by diferent programs that have different record conventions, as you describe, is always a bother. There may be "compiler" options that inform the run time system of your preferences. RTFM very carefully. Some vendors have utilities that will convert the record headers or you can do it yourself if this is going to be an ongoing bother. > Thanks for reading & helping > > Paul
Post Follow-up to this messageOn Mon, 20 Aug 2007 10:26:18 -0700, Paul Hilscher wrote (in article <1187630777.20834.17.camel@Ariel> ): > I think the > problem lies that Fortran-95 uses a 8-byte long record header, while > F-77 uses only 4-byte. But why is this so and what should I do to get > things work ? This is a function of the particular compilers involved - not of f77 versus f95 in general. Unless I missed it, you didn't mention the particular compilers you used. As Gordon mentioned, unformatted I/O is not generally guaranteed to be interoperable between different compilers. This is *NOT* related to f77 versus f90. There were f77 compilers with many, many differen t variants of unformatted file formats... not to speak of the detail that ther e were historically lots of different binary data representations. Things have converged quite a bit today; there are still multiple variants, but not nearly as many as there used to be. I wonder if you are using gFortran for your f95 compiler. Most f95 compilers go out of their way to use a scheme that is compatible with the most common existing practices. I seem to recall that gFortran had (has?) an option related to this, but that the default was to be incompatible. I thought that a poor choice, but... -- Richard Maine | Good judgement comes from experience; email: last name at domain . net | experience comes from bad judgement. domain: summertriangle | -- Mark Twain ________________________________________ ________ Hogwasher, Premier News and Mail for OS X http://www.asar.com/cgi-bin/product.../hogwasher.html ________________________________________ ________
Post Follow-up to this messageRichard Maine wrote: > > I wonder if you are using gFortran for your f95 compiler. Most f95 compile rs > go out of their way to use a scheme that is compatible with the most commo n > existing practices. I seem to recall that gFortran had (has?) an option > related to this, but that the default was to be incompatible. I thought th at > a poor choice, but... If I'm not mistaken, gfortran has somewhat recently switched to using the ifort scheme for extended record sizes, with compatibility options for the original extended record size usage.
Post Follow-up to this messageDnia 20-08-2007, pon o godzinie 15:01 -0400, Craig Powers napisa=C5=82(a): > Richard Maine wrote: ilers=20 mmon=20 =20 that=20 >=20 > If I'm not mistaken, gfortran has somewhat recently switched to using=20 > the ifort scheme for extended record sizes, with compatibility options=20 > for the original extended record size usage. Indeed, with gfortran -frecord-marker=3D4 everything works fine, thanks a lot for the hint !=20 According to the gfortran manual they changed this to be the new default value for version 4.2.1+, so bad luck for me ubuntu came with version 4.1 ;) Paul
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.