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!

buffer in and buffer out 1

Status
Not open for further replies.

alleyor

Programmer
Aug 2, 2009
13
CN
PROGRAM bufferedIoTest
! buffered i/o example: compile with +autodbl
INTEGER a(10)
OPEN ( UNIT = 7, NAME = ’test.dat’, FORM = ’UNFORMATTED’ )
a = (/ (i,i=1,10) /) ! initialize the array A
BUFFER OUT ( 7, 0 ) ( a, a(10) ) ! write out A twice
CALL unit ( 7 )
BUFFER OUT ( 7, 0 ) ( a, a(10) )
CALL unit ( 7 )
! now position the file 40 bytes (5 integer values) into the file
CALL setpos ( 7, 5 )
! read the remainder of the 1st record, and half of the second
BUFFER IN ( 7, 0 ) ( a, a(10) )
WRITE(6,*) a
CLOSE (7)
END PROGRAM bufferedIoTest

This is a program in "HP Fortran 90 Programmer's Reference":It cannot compiler in intel fortran compiler.I want to know how to replace it in ivf,thanks!
 
Use direct access
Code:
PROGRAM directaccess
   INTEGER a(10), ix

   ! Open file, record size same as integer size
   OPEN (7, &
      FILE='test.dat', &
     FORM='UNFORMATTED', &
     STATUS='NEW', &
     ACCESS='DIRECT', &
     RECL=4)
   a = (/ (i, i = 1, 10) /)
   ! Write out the array twice
   ! WRITE(7, REC=1) a will attempt to write all of a to record 1
   ix = 1
   do jj = 1, 2
      do ii = 1, 10
         WRITE (7, REC=ix) a(ii)
         ix = ix + 1
      end do
   end do
   ! Read from the 6th value
   ! READ(7, REC=6) a will attempt to read all of a from record 6
   ix = 6
   do ii = 1, 10
      READ (7, REC=ix) a(ii)
      ix = ix + 1
   end do
   PRINT *, a
   CLOSE (7, STATUS = 'DELETE')
   STOP
END PROGRAM
 
If "OPEN (7, FORM='UNFORMATTED')" is writen "OPEN (7, FORM='FORMATTED')" ,it has a mistake:"formatted i/o to unit open for unformatted transfers",why?
 
Unformatted takes 4 bytes for all integers, 4 bytes for all reals and 8 bytes for all double precision. Formatted needs a format statement to read the data.

If you tell the compiler that the data is formatted, then the program will only read it one line at a time. If it is unformatted, it will return one record at a time.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top