Silverfrost Forums

Welcome to our forums

OPEN statement for binary file

25 Aug 2008 12:24 #3732

I have a code that works with INTEL and COMPAQ VF with the statement

open(16,file=file1,status='old',FORM='BINARY')

but this does not compile with FTN95. What am I missing?

Also, I seem to have some problems with FORM = 'UNKNOWN'. When the file does not exist, the executable is complaining rather than creating the file.

Any guidance?

Thanks

25 Aug 2008 1:23 #3733

Hi,

FORM is only available with 'formatted', 'unformatted' or 'printer'. May be that 'formatted' is what you are looking for. Your second question: Simply use status='unknown'.

Hope that this helps.

25 Aug 2008 1:59 #3735

I will try 'formatted'. From the manual, it doesn't seem to mean the same as binary. It seems to indicate that it is a text file. At any rate, will post here the results.

As to STATUS = 'UNKNOWN', that is what I am doing. Again, I will check once more.

Thank you for the reply.

25 Aug 2008 8:40 #3740

I don't believe that FORM='BINARY' is part of the fortran standard, so it is not surprising that FTN95 does not compile it. For binary files I do as Bruce suggested.

26 Aug 2008 3:22 #3748

Neither 'formatted' nor 'unformatted' work to read a binary file. If there is no other way to do it in FTN95, it would be an interesting feature to add. If there is a way, I would appreciate it if someone would provide some guidance.

26 Aug 2008 3:38 #3749

It would make it easier to understand the problem if you showed a read or write statement associated with this open.

FTN95 can definitely use binary files, but it depends on wheter you mean sequential or direct access.

There has been some recent posts regarding unformatted sequential files and the method FTN95 uses to define the record length. I believe there is no rule in the standard for certain types of binary files, it is compiler dependent. Are you reading a file created by a different compiler?

Regards

Ian PS Topic was https://forums.silverfrost.com/Forum/Topic/732

26 Aug 2008 5:09 #3753

I create the binary file from C#:

BinaryWriter bw = new BinaryWriter(fs);

where fs is a file stream.

I write an integer to it:

bw.Write(securitycode2);

Then, within FTN95, I would like to open that file and read that integer from it:

open(16,file=file1,status='old',FORM='UNFORMATTED') READ(16) ICODENUM
close(16, status = 'DELETE')

As per previous suggestions, I tried both 'formatted' and 'unformatted' in place of the 'binary' I had there before. Neither worked. Am I doing something wrong?

26 Aug 2008 5:30 #3754

As I know nothing about any language starting with the letter 'C', let's make an assumption based on the length of the resulting file. In Fortran terms, we can generally have integers of length 1 byte, 2 bytes and 4 bytes. So, how many characters in your file? If your C# integer is one of the above lengths and the file is also one of those lengths, then you can probably read the data using a direct access read as follows:

For a 1 byte file

integer*1 ICODENUM
open(16,file='whateveryoucalledit.txt',access='direct',recl=1,status='old',iostat=ios)
if(ios .eq. 0)then
  read(16,rec=1)ICODENUM
  print *,icodenum
  close(unit=16,status='delete')
endif

For a two byte change integer1 to integer2 and recl=1 to recl=2 etc.

If C# actually writes Fortran style unformatted files, then you file will have record lengths followed by the data and possibly terminated by the record length, as described in the post I mentioned above. The file will be much longer than just the data, with all the housekeeping stuff as well.

Regards & good luck

Ian PS if you want to view the contents of your file, use a hex-dump program, see post https://forums.silverfrost.com/Forum/Topic/819

26 Aug 2008 9:59 #3755

Have a look at the FTN95 documentation for OPEN, especially for FORM= and ACCESS=.

Unformatted binary files have a record length structure. If you open the file as ACCESS='TRANSPARENT' or as a ACCESS='DIRECT' then you can access all bytes in the file, without FTN95 assuming a record structure.

At the most basic you can use transparent and read 1 byte at a time, to restructure the variable (such as a 4-byte integer). Integer*4 reads work well also.

I've done it in the past and had no problems reading files from unknown sources. The following is a test program related to a previous post. It may help, especially the second part which tests the record structure of the unformatted binary write.

John

program bintest 
implicit none 
!
integer*4 i, iostat, ic, ii(400)
character c*1
!
!test the writing of binary data to a Fortran unformatted file 
!
open (10,file='ftnbintest.out',form='unformatted') 

do i = 1,400
   ii(i) = i
end do
!
write (10) 1 
write (10) 16 
write (10) 32 
write (10) 10281 
write (10) 305419896 
write (10) (ii(i),i=1,400)

close(10) 

open(10,file='ftnbintest.out',form='unformatted',access='TRANSPARENT')
!
do i = 1,99999
   read (10,iostat=iostat) c
   write (*,*) i,ichar(c),iostat
   if (iostat/=0) exit
end do
!
end program bintest 
26 Aug 2008 10:26 #3756

Ian,

Thanks for the post.

I get

68: Field width exceeds direct access record size

when using integer*4 or simply integer, although the number saved to the file is the standard int (32-bit) integer of C#. It is written using .NET's System.IO.BinaryWriter

Looking at the hexa from within VS, the only thing I see after the number is four dots (....). I am not sure whether that is part of the file. What else can I do besides what you suggested?

27 Aug 2008 6:59 #3759

Send me the file!

Please login to reply.