【发布时间】:2017-07-13 23:27:11
【问题描述】:
所以我试图了解一些基本的 fortran IO 并遇到了麻烦。我写了以下
program RFF
implicit none
! Variables
integer :: ierr
character (:), allocatable :: Filename
character (:), allocatable :: read_in
! Body of RFF
Filename = 'string_test.txt'
open(10, file=Filename, status='old', action='read',iostat=ierr) !What is iostat?
do while (ierr.eq.0) !What is this do loop doing exactly?
read(10,'(A)',iostat = ierr) read_in !What does '(A)' mean? I know it's a format descriptor, but nothing beyond that
print*, read_in !Nothing gets output to the terminal here
enddo
write(*,*) 'Press Enter to Exit'
read(*,*)
!Is deallocating dynamically allocatable strings something I should do?
deallocate(Filename)
end program RFF
我提供了一个非常简单的文本文件,其中包含“任意”一词,仅此而已。当我运行程序时,没有任何崩溃,但也没有任何输出到终端。有人可以帮助我了解发生了什么吗?注意我已经在我粘贴的代码的 cmets 中插入了许多其他问题。我也想帮助理解这些。
谢谢
【问题讨论】:
-
你应该在循环之前初始化
ierr=0。不,在程序结束时解除分配毫无意义。 -
修改一下,你应该在打开之后额外检查
ierr,或者最好不要在open中使用iostat。指定iostat然后不对结果做任何事情通常是不好的做法。