Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

The file size is twice bigger when writing real8 into binary file.

zhaopeng
Beginner
676 Views

Hi,

I am a newbie to Fortran language. I try to write a real8 array into binary file using code as follow (compiler: Intel Fortran 2015)

open(unit=219, status='replace',file='data.bin',form='unformatted')
do j=1,ny2dval
do i=1,nx2dval
   write(219) fzavg(i,j)
end do
end do

 

But the file size is twice bigger than expected. It seems to write a real16 array. Why does it happen? Any suggestions?

 

Thanks a lot!

Regards

ZHAO Peng

0 Kudos
5 Replies
mecej4
Honored Contributor III
676 Views

When you write an unformatted sequential file, extra bytes are written to save information about the length of each record. Each length marker is four bytes long, so if you write records that each contain only four bytes of information (the size of a REAL), the record length markers take up as much space as the "payload", so your file will be twice as long. If you wish to reduce the space consumed by the record markers, you could write fewer but longer records. For example, 

write(219) ((fzavg(i,j), i=1,nx2dval), j=1,ny2dval)

would write a single record, assuming that nx2dval and ny2dval are not so big that record size limits are breached.

0 Kudos
zhaopeng
Beginner
676 Views

mecej4 wrote:

When you write an unformatted sequential file, extra bytes are written to save information about the length of each record. Each length marker is four bytes long, so if you write records that each contain only four bytes of information (the size of a REAL), the record length markers take up as much space as the "payload", so your file will be twice as long. 

Thanks for your information!

Actually the file size is not a problem, what I want is to write a binary file using Fortran and read it using C++. So if I use my Fortran codes to generate binary file, the C++ codes should read 8 bytes real value (double precision) and then skip 8 bytes to read next one, right?

Thanks again!

ZHAO Peng

0 Kudos
mecej4
Honored Contributor III
676 Views

If you are not going to use the record length markers, consider opening the file with ACCESS='STREAM' before writing to it. You may also consider the older nonstandard FORM='BINARY' specifier in the file OPEN statement.

0 Kudos
zhaopeng
Beginner
676 Views

mecej4 wrote:

If you are not going to use the record length markers, consider opening the file with ACCESS='STREAM' before writing to it. You may also consider the older nonstandard FORM='BINARY' specifier in the file OPEN statement.

Great! Both ACCESS='STREAM' and  FORM='BINARY'  work. Thank you very much!

0 Kudos
jimdempseyatthecove
Honored Contributor III
676 Views

>> what I want is to write a binary file using Fortran and read it using C++.

Mentioning this in the first post would have resulted in a faster an appropriate answer. It is important that the initial post describes the framework to the question at hand.

Jim Dempsey

0 Kudos
Reply