Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Write works but Read doesn't

Status
Not open for further replies.

BrooksVH

Programmer
Jul 8, 2009
14
US
I've written binary data out but can't seem to read it.
Subroutine RanfPut ( fname )
Character(45),intent(in):: fname
Integer:: i, ok

open ( unit=4,file=fname,iostat=ok,form='unformatted', &
status='replace',position='rewind',access=sequential' )
If (ok > 0) Then
print *, 'RanfPut error on file open name = ', fname, ok
return
Else
write(*,'(a,a)') 'Input file := ', fname
End If
write (4) a0
write (4) a1
write (4) a2
write (4) rvals1
write (4) rvals2
write (4) rvals3
write (4) nsum
close (4)
return
End Subroutine RanfPut

Now the part that doesn't work.
Subroutine RanfGet ( fname )
Character(45),intent(in):: fname
Integer(2):: i, ok
logical(2) :: file_exists

inquire(file=fname, exist=file_exists)
if (.NOT.file_exists) Then
write(*,'(a,a)') 'Input file does not exist. fname = ', fname
return
end if

open (unit=4,file=fname,iostat=ok,form='unformatted', &
status='old',position='rewind',access='sequential')
If (ok > 0) Then
print *, 'RanfGet error on file open name = ', fname, ok
return
Else
write(*,'(a,a)') 'Input file = ', fname
End If

read (4) a0
read (4) a1
read (4) a2
read (4) rvals1
read (4) rvals2
read (4) rvals3
read (4) nsum
close (4)
flag = 1 ! set flag to indicate we are using the table
return
End Subroutine RanfGet

Any help will be greatly appreciated.
Brooks Van Horn
 
Can you post a minimal full program that can be compiled?

A quick look reveals that you are passing " access='sequential' " to the file open commands, but I seem to recall that being incompatible with binary files ('unformatted'); instead, I think you need to pass " access='direct' ". Lastly, I think you are going to need a record length parameter.

What instructions for writing/reading binary have you read / google for? I know they are out there.
 
It seems that the simplest way to go about it is to only specify FORM='unformatted' and forget about ACCESS and RECL (record length). So, the open statement, for example, looks like this:

open ( unit=4,file=fname, status='replace', iostat=ok, form='unformatted')
 

salgerman,

I tried that but got:

Input file = C:\Users\TCS\Desktop\Pearson\MyTest.dat
At line 87 of file Ranf.f95 (unit = 4, file = 'C:\Users\TCS\Desktop\Pearson\MyTe
st.dat')
Fortran runtime error: End of file
 
Baby steps...baby steps...

Please just first practicing writing to a file, proof that you can create a brand new file, writing binary to it and see that the file size is not zero...you can even download a hex editor and inspect the file contents, if you want to.

Once you know how to write, you can move on to reading...But you REALLY need to make sure you read the data in the same exact order and using the same exact variable types you used when you wrote it...and make sure you do not go beyond the end of file. Maybe you can practice reading by knowingly reading less data than you wrote, just to prove that you can read, not risking EOF error.


 
Did the first. When I posted this originally I made sure I could create the file. That is why I entitled the post as 'Write works but Read doesn't'. The only way I can get it to work is with rec=### in the read/write statements.

Brooks
 
So, where do you stand? got it working or not?

I did simply, with:
Code:
! write
   open ( unit=4, file=fname, status='replace', form='unformatted')
   write (4) a0
   write (4) a1
   write (4) a2

!read
   open ( unit=4, file=fname, status='old',     form='unformatted')
   read (4)  a0
   read (4)  a1
   read (4)  a2
 
I never got the binary version to work. Upgraded to 4.9 but still nada.

Brooks

Try this...

real(10)::a0(10), a1(10), a2(10)
real(10):: r(3,10,100)
open(unit=#,...)
write(unit=#) A0
write(unit=#) A1
write(unit=#) A2
write(unit=#) r
close (#)
...
open(unit=#,...)
read(unit=#) a0
read(unit=#) a1
read(unit=#) a2
read(unit=#) r
close (#)

but this doesn't work regardless of open parameters.
 
If you are writting then reading an unformatted file, you have to perform a rewind before you start reading otherwise you get an end of file.

Bill
Lead Application Developer
New York State, USA
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top