CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   Main CFD Forum (https://www.cfd-online.com/Forums/main/)
-   -   Binary in Fortran (https://www.cfd-online.com/Forums/main/12166-binary-fortran.html)

Gerrit September 6, 2006 05:55

Binary in Fortran
 
Does anyone know whether it's possible to save binary files in Fortran?


Robert September 6, 2006 06:03

Re: Binary in Fortran
 
Yes. Use FORM='binary' in the open statement.

Mani September 6, 2006 07:10

Re: Binary in Fortran
 
is there a 'binary' form? how is it different from the standard 'unformatted' form?

you can use form='unformatted' in the open statement, and then simply leave out any format in write statements, i.e.

write(id)

instead of

write(id,'(fmt)')

ag September 6, 2006 07:41

Re: Binary in Fortran
 
The general approach is to specify the use of unformatted direct access storage, as Mani points out. This link has more information.

http://cires.colorado.edu/~knowlesk/...fortranio.html

Some compilers have a FORM=BINARY option, but that appears to be a compiler-specific option, and thus it may not be portable. I don't believe it is part of the ANSI standard.

Gerrit September 6, 2006 08:13

Re: Binary in Fortran
 
It would be sad if it's not ANSI standard, maybe it's ANSI but for F90 only? Maybe someone knows...

Anyway, I'm helped.

Thank you all!

Renato. September 6, 2006 10:23

Re: Binary in Fortran
 
unformatted files are files where the data is stored in a "binary" form (no ascii translation) but with the number of bytes after (or before, I don't remember) each record. This form (unformatted) is intrinsic for all Fortran distributions and you'll not have portability problems with your program. It's some Fortran weirdness because only Fortran has this kind of format. Some compilers (most of the more used, like Intel) have a standard binary form as Robert pointed which turns out the binary files more useful since you can easily read them in programs written in other languages like C/C++.

Regards

Renato.


andy September 8, 2006 04:26

Re: Binary in Fortran
 
A record in a Fortran binary file has an integer header and a footer giving the length of the record. C does not. So you can easily modify a C program to read Fortran binary files but not the other way found.

If you want portable binary files you will have to address the endianess issue which is easy enough to fix. You can also come unstuck with how floating point numbers are stored although this is less of an issue these days.

If you want the speed and compactness of binary files but portability as well then a good approach is often to store your data in XDR format.



All times are GMT -4. The time now is 08:55.