Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Io Fortran从文件读取字符-无输出_Io_Fortran - Fatal编程技术网

Io Fortran从文件读取字符-无输出

Io Fortran从文件读取字符-无输出,io,fortran,Io,Fortran,所以我试图理解一些基本的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, st

所以我试图理解一些基本的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
我输入了一个非常简单的文本文件,其中包含单词“arbitral”,没有其他内容。当我运行程序时,没有崩溃,但是也没有输出到终端。有人能帮我理解发生了什么事吗?注意,我在粘贴的代码注释中插入了许多其他问题。我也想帮助理解这些


谢谢

在您的“一个代码”中有两个问题,所以让我先解决它们:

什么是iostat?-iostat是与I/O语句相关的错误代码。如果一切正常,它将被设置为0,如果没有,它将有一个非零值。尝试打开文件时可能出错的示例:

文件不存在 目录不存在 用户没有打开该文件的权限 什么是A这是格式字符串。A指任意长度的字符串。有关更多信息,请参阅

代码看起来应该可以工作,但是结果并不是您所期望的。我想到了两种可能性:

您试图读取的文件不存在,或者在执行程序的目录中不存在,或者您没有读取该文件的权限。这将导致ierr设置为与“未找到文件”对应的非零错误代码。这导致do循环甚至不执行一次,因为ierr一开始就不是零

我建议在iostat之外使用更新的iomsg语句。iomsg是一个字符串,对应于对出错原因的可读解释

character(len=100) :: iomsg
<snip>
open(10, file=Filename, status='old', action='read',iostat=ierr, iomsg=iomsg)
if (ierr /= 0) then
    print*, "Error opening file " // trim(Filename)
    print*, iomsg
    STOP 1
end if
文件存在,但为空。这将导致不打印任何内容。只需检查以确保文件中没有任何更改


提示,iostat和iomsg都可用于所有文件I/O操作,因此也可用于读取、写入甚至关闭。使用它们不会有什么坏处。

真正的问题是,必须先分配read\u-in,然后才能使用read分配给它。还有一件事:iostat用于指示完成状态或可能的错误情况。有关其他详细信息,请参见代码注释和官方文档,例如

以下是一个可行的解决方案:

program main
    implicit none

    character(len=20) :: read_in                     ! fixed-length string
    character(len=:), allocatable :: word, filename  ! allocatable strings
    integer :: iostat_1, iostat_2                    ! status indicators
    integer :: lun                                   ! file logical unit number

    filename = 'read_test.txt'                       ! allocate on assignment
    iostat_1 = 0                                     ! initialization
    iostat_2 = 0

    open(newunit=lun, file=filename, status='old', iostat=iostat_1)
    if (iostat_1 == 0) then                          ! no error occurred
        do while(iostat_2 == 0)                      ! continues until error/end of file
            read(lun, '(a)', iostat=iostat_2) read_in
            if (iostat_2 == 0) then
                word = trim(read_in)                 ! allocate on assignment to trimmed length.
            endif
        enddo
        if (allocated(word)) then
            print *, "read_in:", read_in
            print *, "len(read_in)", len(read_in)
            print *, "Word:", word
            print *, "len(word)=", len(word)
        else
            print *, "Word was not allocated!"
        endif
    endif
end program main
示例输出:

 read_in:arbitrary
 len(read_in)          20
 Word:arbitrary
 len(word)=           9

您应该在循环之前初始化ierr=0。不,在程序结束时取消分配是没有意义的。修改这一点,您应该在打开后额外检查ierr,或者最好不要在打开时使用iostat。通常情况下,指定iostat而不对结果执行任何操作都是不好的做法。为什么不指定lun?“你只需将其设置为任意数字,对吗?”将军说,“你观察力很强。”。2008标准中的newunit说明符打开一个未使用的单元号文件,并返回所选的编号。我已将该值分配给lun,以便以后可以使用read访问正确的单元号。因此,当您编写open命令时,lun会自动分配?而且我似乎无法在可分配阵列上执行读取操作。这意味着,基本上,我无法读入可分配数组。为什么呢?这是个人的,但我永远不想读到需要预先分配的东西。我的假设是,这是因为您试图读入的未分配字符串的长度为零,所以read是有效的,它只是试图存储到一个数组中,实际上是零空格。是否有动态分配的方法?例如,读入字符数或一次读入一个字符并追加,等等?从newunit向lun分配一个值,这是一个返回未使用的单元号的函数。在大多数情况下,值是什么并不重要,只要您有一个对它的引用。