View previous topic :: View next topic |
Author |
Message |
RS
Joined: 22 Jun 2008 Posts: 22
|
Posted: Mon Aug 25, 2008 1:24 pm Post subject: OPEN statement for binary file |
|
|
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 |
|
Back to top |
|
 |
Wilfried Linder
Joined: 14 Nov 2007 Posts: 314 Location: D�sseldorf, Germany
|
Posted: Mon Aug 25, 2008 2:23 pm Post subject: |
|
|
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. |
|
Back to top |
|
 |
RS
Joined: 22 Jun 2008 Posts: 22
|
Posted: Mon Aug 25, 2008 2:59 pm Post subject: FORM and STATUS |
|
|
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. |
|
Back to top |
|
 |
brucebowler Guest
|
Posted: Mon Aug 25, 2008 3:26 pm Post subject: |
|
|
For binary, you do probably want UNFORMATTED, not FORMATTED |
|
Back to top |
|
 |
JohnHorspool
Joined: 26 Sep 2005 Posts: 270 Location: Gloucestershire UK
|
Posted: Mon Aug 25, 2008 9:40 pm Post subject: |
|
|
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. |
|
Back to top |
|
 |
RS
Joined: 22 Jun 2008 Posts: 22
|
Posted: Tue Aug 26, 2008 4:22 pm Post subject: Neither 'formatted' nor 'unformatted' work to read a binary |
|
|
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. |
|
Back to top |
|
 |
IanLambley
Joined: 17 Dec 2006 Posts: 506 Location: Sunderland
|
Posted: Tue Aug 26, 2008 4:38 pm Post subject: |
|
|
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 http://forums.silverfrost.com/viewtopic.php?t=978 |
|
Back to top |
|
 |
RS
Joined: 22 Jun 2008 Posts: 22
|
Posted: Tue Aug 26, 2008 6:09 pm Post subject: Binary files |
|
|
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? |
|
Back to top |
|
 |
IanLambley
Joined: 17 Dec 2006 Posts: 506 Location: Sunderland
|
Posted: Tue Aug 26, 2008 6:30 pm Post subject: |
|
|
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
Code: |
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 integer*1 to integer*2 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
http://forums.silverfrost.com/viewtopic.php?t=1068 |
|
Back to top |
|
 |
JohnCampbell
Joined: 16 Feb 2006 Posts: 2621 Location: Sydney
|
Posted: Tue Aug 26, 2008 10:59 pm Post subject: |
|
|
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
Code: |
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
|
|
|
Back to top |
|
 |
RS
Joined: 22 Jun 2008 Posts: 22
|
Posted: Tue Aug 26, 2008 11:26 pm Post subject: 68: Field width exceeds direct access record size |
|
|
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? |
|
Back to top |
|
 |
IanLambley
Joined: 17 Dec 2006 Posts: 506 Location: Sunderland
|
Posted: Wed Aug 27, 2008 7:59 am Post subject: |
|
|
Send me the file! |
|
Back to top |
|
 |
|