Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Write works but Read doesn't

Write works but Read doesn't

Write works but Read doesn't

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
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)
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
end if

open (unit=4,file=fname,iostat=ok,form='unformatted', &
If (ok > 0) Then
print *, 'RanfGet error on file open name = ', fname, ok
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
End Subroutine RanfGet

Any help will be greatly appreciated.
Brooks Van Horn

RE: Write works but Read doesn't

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.

RE: Write works but Read doesn't

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')

RE: Write works but Read doesn't



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
Fortran runtime error: End of file

RE: Write works but Read doesn't

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.

RE: Write works but Read doesn't

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.


RE: Write works but Read doesn't

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

I did simply, with:


! write
   open ( unit=4, file=fname, status='replace', form='unformatted')
   write (4) a0
   write (4) a1
   write (4) a2

   open ( unit=4, file=fname, status='old',     form='unformatted')
   read (4)  a0
   read (4)  a1
   read (4)  a2 

RE: Write works but Read doesn't

I never got the binary version to work. Upgraded to 4.9 but still nada.


Try this...

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

but this doesn't work regardless of open parameters.

RE: Write works but Read doesn't

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.

Lead Application Developer
New York State, USA

RE: Write works but Read doesn't

Thanks, it works.


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close