繁体   English   中英

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

[英]Fortran Reading characters from file - no output

因此,我试图了解一些基本的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

我已经提供了非常简单的文本文件,其中包含单词“任意”,仅此而已。 当我运行程序时,没有崩溃,但也没有输出到终端。 有人可以帮助我了解发生了什么吗? 注意,我在粘贴的代码的注释中插入了其他一些问题。 我也想帮助他们。

谢谢

真正的问题是,在使用read分配read_in之前,必须先分配read_in 另一件事: 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

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

  • 什么是iostat iostat是与I / O语句有关的错误代码。 如果一切正常,则将其设置为0 ;否则,它将具有非零值。 有关尝试打开文件时可能出错的示例:

    • 文件不存在
    • 目录不存在
    • 用户无权打开文件
  • 什么是(A) -这是格式字符串。 (A)是指任意长度的字符串。 看到这里更多信息

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

  1. 您尝试读取的文件不存在,或者您正在执行程序的目录中不存在该文件,或者您没有读取该文件的权限。 这导致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 
  2. 该文件存在,但为空。 这将导致不打印任何内容。 只需检查以确保文件中没有任何更改。

提示, iostatiomsg在所有文件I / O操作中都可用,因此也可以进行readwrite甚至close 使用它们不会有伤害。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM