Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
335 views
in Technique[技术] by (71.8m points)

compilation - Why direct access I/O works incorrectly with Intel Visual Fortran

This program

  program test

  real a(10)

  open(1, file='f1',access='direct', recl=20)
  do i=1, 10
      a(i) = i-1
  end do
  write(1, rec=1)(a(i),i=1,5)
  write(1, rec=2)(a(i),i=6,10)
  close(1)
  open(1, file='f1',access='direct',recl=8)
  read(1, rec=4)(a(i),i =5,9,4)
  print*,a
  end

works incorrect in visual Fortran (incorrect):

  0.0000000E+00   1.000000       2.000000       3.000000       9.000000
   5.000000       6.000000       7.000000      0.0000000E+00   9.000000

Result in WATCOM (correct):

  0.0000000   1.0000000       2.0000000       3.0000000       6.0000000
  5.0000000   6.0000000       7.0000000       7.0000000       9.0000000

Why?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should not need to know when writing the code how exactly are the record long in whatever units. Setting recl=20 or recl=5 and depending on exact behaviour of your compiler and exact real size is a recipe for future problems.

You should ask the compiler for the size of the record in whichever units it uses:

integer :: recl5, recl2

inquire(iolength=recl5) (a(i),i=1,5)

open(1, file='f1',access='direct', recl=recl5)


...


inquire(iolength=recl2) (a(i),i =5,9,4)
open(1, file='f1',access='direct',recl=recl2)

Note: In Fortran 90 it could be much shorter by using subarray notation.

Note2: For some compilers your program will never work as the compiler is allowed to use record end marks into the file and you have to always open it with the same recl. But that is not common.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...