Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29276 Discussions

Can I read the binary file from the give position?

Zhanghong_T_
Novice
647 Views
Hi all,
I want to realize the following function:
Firstly, I write a 'binary' file (real*8 data type), then I wish I can locate to any position of the file and read the correspondent data without read from the begin of the file? How can I do? My code is as following. Can anyone modify the code for me?
Thanks,
Zhanghong Tang
Code:
implicit none
real*8 a
integer i
open(1,file='test',form='binary')
do i=1,10000
  a=i
  write(1)a
enddo
call testreadfile(1)
close(1)
end

subroutine testreadfile(fileid)
integer i
integer fileid
do i=1,10000
  a=readfile(fileid,i)
enddo
end subroutine

real*8 function readfile(fileid,i)result(out)
integer fileid, i
! How can I read the ith data from the file without read all its former data?
! Thanks

end function
0 Kudos
4 Replies
Steven_L_Intel1
Employee
647 Views
Read about the FSEEK library function in the Intel Fortran Libraries Reference.
0 Kudos
Paul_Curtis
Valued Contributor I
647 Views
This excellent question illustrates the limitations of Fortran file i/o as compared to other languages. Perhaps someday Fortran will develop modern file i/o. In the meantime,Win32 API functions can completely replace the native F90 file i/o, with improved performance and total versatility (albeit a lack of portability). See the attached code, particularly SetFilePointer.
0 Kudos
Zhanghong_T_
Novice
647 Views
Thank both of you very much!
Another question: Can the 32-bit system support the file size greater than 2G?
Thanks,
Zhanghong Tang
0 Kudos
Steven_L_Intel1
Employee
647 Views
Yes.
0 Kudos
Reply