That cannot work, no surprise as this makes the array of default (typically 4-bytes) reals.
As for the above, I do not quite catch what is going on. Your sample Fortran program uses unformatted
file which by default, at least in gfortran
is made of records, with the binary (unformatted) data preceded and followed by the record length in bytes. These record lengths are (gfortran v.11
) 4 bytes each! Consider your code slighthly modified to create the file instead of reading it:
writeit
Program writeit
Implicit None
Real(8), Dimension( -1000:1000, 0:50*1000 ) :: VArPolar
Integer :: i, j
call random_number(VArPolar)
VArPolar(-1000,0) = 0d0
VArPolar(1000,50000) = 0d0
Open( 10,FILE='BaPolar', FORM='unformatted')
write ( 10) VArPolar
Close( 10 )
write (*,*) VArPolar(0,4500)
End Program writeit
Hex dump of the file (first and last line) shows:
00000000 08 61 b5 2f 00 00 00 00 00 00 00 00 48 e2 10 3e
...
2fb56100 b2 44 97 3f 00 00 00 00 00 00 00 00 08 61 b5 2f
You can clearly see the 4-byte lenghts of record (0x2fb56108 = 800416008). The total file size is 800416016 bytes, not just 800416008 as you wrote before.
When I manually deleted the 4-byte lenghts from the beginning and the end of file, your readit
program failed showing
At line 7 of file unform.f90 (unit = 10, file = ‘BaPolar’)
Fortran runtime error: I/O past end of record on unformatted file
But this maybe due to zeroes being interpreted as record length, so YMMV.
If your file indeed contains the record lengths (4-bytes), the failure of the C code would be obvious as it reads binary data without any record info, so it would make double values of two 4-byte parts coming from different original values (4-byte shift!). Still, it does not explain how your file gets properly read by Fortran program, if it really has 800,416,008 bytes only.