我有一个未格式化的 Fortran 文件,其中包含不同长度的字符串,并且我无法使用 Fortran 本身读取这些字符串。
示例程序:
program test
implicit none
character(len=200) :: line
open(32,file="testfile",form="unformatted",action="write")
write(32) "A test string"
write(32) "Another longer test string"
close(32)
open(33,file="testfile",form="unformatted",action="read")
read(33) line
write(6,*) trim(line)
read(33) line
write(6,*) trim(line)
close(33)
end program test
这失败(用 gfortran 编译):
At line 11 of file test.f90 (unit = 33, file = 'testfile')
Fortran runtime error: I/O past end of record on unformatted file
我可以通过尝试减少长度和退格( read_string
子例程)的读取来使其工作,但这看起来效率非常低:
program test
implicit none
character(len=200) :: line
open(32,file="testfile",form="unformatted",action="write")
write(32) "A test string"
write(32) "Another longer test string"
close(32)
open(33,file="testfile",form="unformatted",action="read")
call read_string(33,line)
write(6,*) trim(line)
call read_string(33,line)
write(6,*) trim(line)
close(33)
contains
subroutine read_string(u,string)
integer, intent(in) :: u
character(len=*), intent(out) :: string
integer :: i, error
do i=len(string),0,-1
read(u,iostat=error) string(:i)
if (error == 0) then
string(i+1:) = ''
exit
end if
backspace(u)
end do
end subroutine read_string
end program test
有没有更好的方法从未格式化的文件中读取可变长度字符串?
我稍微修改了您的示例程序,读取了
binary
中的文件。这适用于英特尔的编译器; gfortran 不知道二进制格式,所以 ymmv。在 Intel's reference on record types 看看我的想法是从哪里来的它的输出是
既然您知道了行的长度,
trim()
就不再是必需的了。只需使用这也可以防止在向数据添加“较短的测试字符串”时出现问题。