Io 向后扫描格式化的流文件
格式化流I/O有更多的问题。这次我扫描了一个为格式化流访问而打开的文件,查找行的开头。这是完全成功的。但是,当我尝试使用这些标记向后复制文件时,在Io 向后扫描格式化的流文件,io,fortran,gfortran,intel-fortran,Io,Fortran,Gfortran,Intel Fortran,格式化流I/O有更多的问题。这次我扫描了一个为格式化流访问而打开的文件,查找行的开头。这是完全成功的。但是,当我尝试使用这些标记向后复制文件时,在ifort和gfortran上都会遇到问题。该方案: program testpos2 implicit none character(80) halfline character(:), allocatable :: line integer iunit integer junit integer i int
ifort
和gfortran
上都会遇到问题。该方案:
program testpos2
implicit none
character(80) halfline
character(:), allocatable :: line
integer iunit
integer junit
integer i
integer, parameter :: N = 4
integer pos(N)
integer size
integer current
open(newunit=iunit,file='testpos2.txt',status='replace')
do i = 1, N
write(halfline,'(*(g0))') i,' 2nd half ',i
write(iunit,'(*(g0))') trim(halfline)
end do
close(iunit)
allocate(character(len_trim(halfline)) :: line)
open(newunit=iunit,file='testpos2.txt',status='old',access='stream',form='formatted')
do i = 1, N
inquire(iunit,pos=pos(i))
read(iunit,'()')
end do
open(newunit=junit,file='testpos3.txt',status='replace')
do i = N, 1, -1
read(iunit,'(a)',pos=pos(i),advance='no',eor=10,size=size) line
inquire(iunit,pos=current)
write(*,'(*(g0))') 'pos(i) = ',pos(i),', current = ',current
write(junit,'(*(g0))',advance='no') line
do
read(iunit,'(a)',advance='no',eor=10,size=size) line
inquire(iunit,pos=current)
write(*,'(*(g0))') 'pos(i) = ',pos(i),', current = ',current
write(junit,'(*(g0))',advance='no') line
end do
10 continue
write(junit,'(*(g0))') line(1:size)
end do
end program testpos2
带有gfortran 8.1.0的运行时输出:
pos(i) = 43, current = 55
pos(i) = 29, current = 41
pos(i) = 15, current = 27
pos(i) = 1, current = 13
太好了!但输出文件是:
4 2nd half 4
3 2nd half 3
4 2nd half 4
1 2nd half 1
注意第三行是错误的!
使用ifort 16.0.2的运行时输出:
pos(i) = 43, current = 55
pos(i) = 29, current = 69
forrtl: severe (24): end-of-file during read, unit -130, file C:\testpos2.txt
输出文件在第一行之后是二进制垃圾文件
这看起来很像内存损坏的结果,但程序似乎很简单,我看不出哪里会发生损坏,更不用说ifort和gfortran在向前扫描文件时都会产生预期的结果。那么,是两个编译器的I/O库都导致了损坏还是我造成了损坏?并不是说这会造成很大的不同,而是尝试设置iunit和junit。@cup我不确定您的意思:带有NEWUNIT=
连接规范设置iunit
和junit
的OPEN
语句。好的,我试着设置了iunit=10
和junit=11
,然后切换到UNIT=
connect规范,结果与之前相同。