From: Tobias Burnus on
On 05/07/2010 01:16 PM, feenberg wrote:
> Unless the file was written by a Fortran program from the same
> compiler, "unformatted" will not be able to read the file.

Well, the situation is not that bad; often a file written with one
Fortran compiler can also be opened using a different compiler - but,
granted, it does not always work. One also need to watch out for endian
differences, if one transfers files from a different platform, but with
compiler options/environment variables, one can usually handle those.

Tobias
From: Richard Maine on
feenberg <feenberg(a)gmail.com> wrote:

> To read files from another source
> a direct access file with a record length of 1 is possible.

Sometimes. There really isn't any completely bullet-proof and portable
method before the access='stream' of f2003. There are multiple
situations in which a record length of 1 won't work.

There are compilers for which the 1 means 1 word instead of 1 byte
(although there tend to be options to change that, at least with recent
compilers).

There are systems where the record length can't be arbitrary (for
example, it might need to be a multiple of the word length) or where
some record lengths imply record padding.

There are compilers where direct access files also have file or record
headers instead of being pure data.

There is also the mess of having to put together the 1-byte pieces after
you have read them. Don't assume that you can, for example, just read a
4-byte real with a record length of 1 byte, causing the compiler to read
4 records. That trick is non-standard and non-portable.

All that being said, things will often, perhaps even usually, work
(assuming you really do read 1 byte at a time and do asembly
separately). But there are enough caveats that I consider it misleading
to imply that it works without qualification.

Stream acess is *MUCH* more straightforward and has far fewer caveats;
that's why it was added.

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain
From: lumbot on
Thanks everyone,
The file was created by IDL into a binary in Mac Snow Leopard; I am
reading the file on linux. Endian is the same.
I don't think there is any first record indicating the number of
bytes. There are 7*691201 records of 4 bytes --> 19353628 that is the
file size.
----
Somehow 'sequential' does not work. Maybe I am using F90 (not F2003)

open(unit=2,file=fin,status='old',ACCESS='stream',FORM='UNFORMATTED',iostat=open_status)
if(open_status>0)stop"unit2 open error"
> ./mk_07orb_1sec
STOP unit2 open error
----
od -x xx.bin | head
0000000 0000 0000 4cee 45db d8c8 c3ee bcd3 441f
0000020 b7c8 3f1b 14b6 bf8e 0638 c0ed 0001 0000
0000040 51c3 45db 66d4 c3ef e2c1 441d aec5 3f19
0000060 02f3 bf8e 0c22 c0ed 0002 0000 5689 45db
0000100 f4ce c3ef 08a3 441c a5b1 3f17 f128 bf8d
0000120 11f9 c0ed 0003 0000 5b3e 45db 82b6 c3f0
0000140 2e79 441a 9c9d 3f15 df55 bf8d 17c0 c0ed
0000160 0004 0000 5fe2 45db 108d c3f1 5444 4418
0000200 9379 3f13 cd79 bf8d 1d73 c0ed 0005 0000
0000220 6477 45db 9e51 c3f1 7a03 4416 8a55 3f11
----
I tried to read in direct access mode
integer(4)isec
real(4)scpos15(3),scvel15(3)

open(unit=2,file=fin,status='old',ACCESS='direct',FORM='UNFORMATTED',iostat=open_status,recl=4)
if(open_status>0)stop"unit2 open error"
read(2,rec=1)isec
read(2,rec=2)scpos15 (this is line 27)
read(2,rec=5)scvel15
the first record, isec, is now read correctly, but not the rest
> ./mk_07orb_1sec
At line 27 of file mk_07orb_1sec.f90
Fortran runtime error: Short record on unformatted read






From: Louis Krupp on
On 5/7/2010 7:01 AM, lumbot wrote:
> Thanks everyone,
> The file was created by IDL into a binary in Mac Snow Leopard; I am
> reading the file on linux. Endian is the same.
> I don't think there is any first record indicating the number of
> bytes. There are 7*691201 records of 4 bytes --> 19353628 that is the
> file size.
> ----
> Somehow 'sequential' does not work. Maybe I am using F90 (not F2003)
>
> open(unit=2,file=fin,status='old',ACCESS='stream',FORM='UNFORMATTED',iostat=open_status)
> if(open_status>0)stop"unit2 open error"
>> ./mk_07orb_1sec
> STOP unit2 open error

Try it without the iostat clause. You'll get a more informative message.

Louis
From: Gordon Sande on
On 2010-05-07 03:15:29 -0300, lumbot said:

> I am getting the following error:
> Fortran runtime error: I/O past end of record on unformatted file
>
> The codes are:
> character(100)fin
> real(4)a
> fin='xx.bin'
>
> open(unit=2,file=fin,status='old',ACCESS='SEQUENTIAL',FORM='UNFORMATTED')
> read(2)a
> write(*,*)a
> close(2)
>
> The file exists
>> l xx.bin
> -rw-r--r-- 1 -- staff 19353628 May 5 14:01 ./xx.bin
>
> Any clue

Standard issues with very poorly asked question:

1. What system? GNU Fortran supports many. Which GNU Fortran? G95 and Gfortran
are both "GNU Fortran"s and both claim to be fortran 95.

2. Where did the unformatted file come from? What program wrote it? Do you
really know what "unformatted" means as a Fortran technical adjective? It
is what others call "binary" (but all files are binary on a binary computer so
common descriptions are pretty flakey at best).

3. Have you octal/hex dumped the "unformatted" file to verify that it
follows the
Fortran rule for being an unformatted file. The fortran rules are very strict
to support long records, backspacing, partial reading etc.

4. Did you think you were getting a byte sequential read which is Fortran 2003
and has various names as an extension to earlier versions.



First  |  Prev  |  Next  |  Last
Pages: 1 2 3
Prev: Fortran 77 Manual for beginners
Next: Fortran 95 Tutorial