Dear Dr. Wang:
The two programs write output files that are structured differently.
* Program 1 writes a file that consists of one record.
* Program 2 writes a file that consists of two records.
When writing to a sequential unformatted file, each READ or WRITE statement
reads form or writes to exactly one record (Fortran 95 standard, sec.
9.4.4.4.1).
Thus, the difference in lengths between the two files comes from the
record delimiters in each of the two files. The method that the operating
system and file system use for marking records and specifying their lengths
also accounts for the difference in the first 4 bytes and last 4 bytes. I
should point out that exactly how records are marked is processor-dependent,
i.e., it depends on the **combination** of the compiler, operating system,
and file system.
You cannot write out an array using one WRITE statement and then read it
in using two separate READ statements. This is because of the rule above.
Also, non-advancing I/O is allowed only in FORMATTED I/O. This restriction
will be modified somewhat in Fortran 2000, thru the use of Stream I/O.
Hope this helps.
Xiaogang Wang wrote:
> Hello,
>
> I found the following program 1 and 2 give different binary files.
> Could anyone explain why? I compared the two binary files
> and find that test.out2 has 8 bytes more in the middle and the first
> 4 bytes and last 4 bytes of the two files are also different.
>
> Related with this, I have a binary file produced by
> write(11) A
>
> Can I read it in column by column in a different code by doing
> read(11)A(:,1)
> read(11)A(:,2)
>
> The need for doing this comes when A is a huge array, and I only need it
> one column at a time.
>
> Thanks for your help
>
> Xiaogang
>
> program 1
> ===========A
> real*8 :: A(1000,2)
> open(11,file='test.out1',access='sequential',form='unformatted')
> write(11) A
> close(11)
>
> program 2
> ===========
> real*8 :: A(1000,2)
> open(11,file='test.out2',access='sequential',form='unformatted')
> write(11) A(:,1)
> write(11) A(:,2)
> close(11)
>
> ------------------------------------------------
> Dr Xiaogang Wang
> Departement de chimie
> Universite de Montreal
> C.P. 6128, succursale Centre-ville
> Montreal, Quebec H3C 3J7
>
> Tel. (514) 3436111 ext. 3947 (office)
> FAX (514) 3437586 (office)
> e-mail: [log in to unmask]
> homepage: http://www.esi.umontreal.ca/~wangx
> ------------------------------------------------
--
----------
Sincerely,
Craig T. Dedo
17130 W. Burleigh Place Internet: [log in to unmask]
Brookfield, WI 53005-2759 Voice Phone: (262) 783-5869
USA Fax Phone: (262) 783-5928
"They that can give up essential liberty to obtain a little temporary
safety deserve neither liberty nor safety." -- Benjamin Franklin
(1759)
|