On Thu, 2009-02-19 at 17:18 -0800, Yasuki Arasaki wrote:
> M.R.Hadian wrote:
> >> I want to write data in a binary file. the problem is that the data are
> >> mixed of text and numbers. as i know, Binary files can be used with
> >> UNFORMATTED form. a sample of my file in ASCII format is attached. this
> >> file is used by my visualization software (tecplot). the software has a
> >> small program that change the data to binary which cause faster access
> >> to file and less file size, However i want to do it in my fortran code.
On some systems, with some compilers, if you have fixed-length records,
you can fake stream output using direct access.
Get the record size using "inquire by iolength:"
INQUIRE ( IOLENGTH=len ) x, y, z....
where "len" will get the correct value to put into the RECL field of an
OPEN statement, and x, y, z... is the output list you'll be putting in
your write statements. Then...
OPEN ( 11, access='direct', form='unformatted', recl=len )
nrec = 0
do...
...
nrec = nrec + 1
write ( 11, rec=nrec ) x, y, z, ....
...
end do
> Fortran unformatted sequential files OPENed with FORM="UNFORMATTED"
> usually include record lengths inside the file in addition to the
> data you WRITE, so is not suited for writing binary files for use
> outside of the Fortran program. You want to OPEN with ACCESS="STREAM"
> instead:
>
> program binary_example
>
> open(11,file="test",access="stream")
> write(11) int(z"01234567")
> write(11) "ABCD"
> close(11)
> end program binary_example
>
> This program outputs the 8 bytes {67 45 23 01 41 42 43 44} on
> gfortran 4.3 on an i386 FreeBSD.
>
> Unfortunately, this Fortran 2003 Standard feature may not yet be
> implemented on the compiler you use. But you'll probably be able to
> find something similar implemented in a way that varies from compiler
> to compiler. You'll need to look it up in your compiler manual, or ask
> the question with the compiler name and version.
>
> For example, in a version of Intel Fortran, the OPEN is written:
>
> open(11,file="test",form="unformatted",recordtype="stream")
>
> Once you OPEN right, WRITE without a format is what you use, as
> Robin Vowels said in an earlier post.
--
Van Snyder | What fraction of Americans believe
[log in to unmask] | Wrestling is real and NASA is fake?
Any alleged opinions are my own and have not been approved or
disapproved by JPL, CalTech, NASA, the President, or anybody else.
|