- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
>> 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
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page