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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to read a binary file without 'FORM=binary' 1

Status
Not open for further replies.

skwattrinated

Technical User
Mar 2, 2005
7
IT
Hi
I need to read binary files (record lenght = 4 bytes) but I use g95, so I can't use the "form='binary'" statement. I have to rewrite data in a matrix [ncols x nrows]. I tried with:

Code:
open(unit=10,file=file_in,recl=4)
do row=1,nrows
  read(10) (dati(col,row),col=1,ncols)
  write(*,*) (dati(col,row),col=1,ncols)
enddo
close(10)

but it doesn't work.
Any suggestion?
Thanks all
 
I just tried.
At run-time I obtain:
Code:
 2 [main] Prova 240 handle_exceptions: Exception: STATUS_ACCESS_VIOLATION
19142 [main] Prova 240 open_stackdumpfile: Dumping stack trace to Prova.exe.stackdump
I'm not an expert, I don't understand what it means...
 
See if the following works for you. You need to copy zapotec.bmp from your windows directory (or use any bitmap: just change the filename)
Code:
program main
   integer:: bmp
   character*2:: bfType
   integer*4:: bfSize, bfReserved, bfOffBits
   integer*4:: biSize, biWidth, biHeight
   !
   ! Get zapotec.bmp from the windows directory
   bmp = 23 ! any number will do
   open ( &
      unit = bmp, &
      file = 'zapotec.bmp', &
      status = 'old', &
      form = 'unformatted', &
      access = 'direct', &
      recl = 128)

   ! read the header of the binary file
   read (unit = bmp, rec = 1) &
      bfType, bfSize, bfReserved, bfOffBits, & ! Header
      biSize, biWidth, biHeight
   print *, 'The bitmap is ', biWidth, ' x ', biHeight
   close (bmp) 
   stop
end program main
Binary I/O is particularly difficult in that the record length dictates the granularity. If you have a record length, access has to be direct. On g77, you could get away with a record length of 1 but on g95, you can't.
 
Just tried your stuff. The problem seems to be in the implied loop - it assumes it is all in the same record so this needs to be broken up into a proper loop.
Code:
program main
   integer:: bmp
   integer:: ncols, nrows, recs
   parameter (ncols = 5, nrows = 5)
   integer, dimension(ncols,nrows):: dati
   integer:: col, row
   !
   ! Get zapotec.bmp from the windows directory
   bmp = 23 ! any number will do
   open ( &
      unit = bmp, &
      file = 'zapotec.bmp', &
      status = 'old', &
      form = 'unformatted', &
      access = 'direct', &
      recl = 4)

   recs = 0
   do row=1,nrows
      do col = 1, ncols
         recs = recs + 1
         read(unit = bmp, rec = recs) dati(col,row)
      enddo
      write(*,'(5Z9)') (dati(col,row),col=1,ncols)
   enddo
   close (bmp) 
   stop
end program main
 
Thank you!!!
Your last code is the solution for my problems.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top