简体   繁体   English

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

[英]Fortran Reading characters from file - no output

So I'm trying to understand some basic fortran IO and having trouble. 因此,我试图了解一些基本的fortran IO并遇到麻烦。 I've written the following 我写了以下

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

Which I've fed the very simple text file containing the word 'arbitrary' and nothing else. 我已经提供了非常简单的文本文件,其中包含单词“任意”,仅此而已。 When I run the program, nothing crashes but nothing gets output to the terminal, either. 当我运行程序时,没有崩溃,但也没有输出到终端。 Can someone help me understand what is going on? 有人可以帮助我了解发生了什么吗? Note I've inserted a number of other questions into the comments of the code I've pasted. 注意,我在粘贴的代码的注释中插入了其他一些问题。 I'd like help understanding those as well. 我也想帮助他们。

Thanks 谢谢

The real problem is that you must allocate read_in before you assign to it with read . 真正的问题是,在使用read分配read_in之前,必须先分配read_in One other thing: iostat is used to indicate either completion status or a possible error condition. 另一件事: iostat用于指示完成状态或可能的错误情况。 See the code comments and official docs for other details (for example, here ). 有关其他详细信息,请参见代码注释和官方文档(例如, 此处 )。

Here is a working solution: 这是一个可行的解决方案:

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

Example output: 输出示例:

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

There are two questions in your one code, so let me address them first: 您的一个代码中有两个问题,所以让我首先解决它们:

  • What is iostat ? 什么是iostat -- iostat is an error code relating to the I/O statement. iostat是与I / O语句有关的错误代码。 If everything worked fine, it will be set to 0 , if not, it will have a non-zero value. 如果一切正常,则将其设置为0 ;否则,它将具有非零值。 Examples on what could go wrong when trying to open a file: 有关尝试打开文件时可能出错的示例:

    • File doesn't exist 文件不存在
    • Directory doesn't exist 目录不存在
    • User doesn't have permission to open the file 用户无权打开文件
  • What is (A) ? 什么是(A) -- This is the format string. -这是格式字符串。 (A) refers to a string of arbitrary length. (A)是指任意长度的字符串。 See here for more information 看到这里更多信息

The code looks as if it should work, however the result isn't what you expect. 该代码看起来好像应该工作,但是结果不是您期望的。 There are two possibilities that spring to my mind: 我想到了两种可能性:

  1. The file you are trying to read doesn't exist, or doesn't exist in the directory that you are executing the program in, or you don't have the permission to read it. 您尝试读取的文件不存在,或者您正在执行程序的目录中不存在该文件,或者您没有读取该文件的权限。 This results in ierr being set to a non-zero error code (corresponding to 'File not Found'). 这导致ierr设置为非零错误代码(对应于“未找到文件”)。 This results in the do loop not executing even once since the ierr isn't zero to start with. 这导致do循环甚至不会执行一次,因为ierr并非从零开始。

    I recommend using the newer iomsg statement in addition to iostat . 除了iostat之外,我建议使用更新的iomsg语句。 iomsg is a string that corresponds to a human-readable explanation to what went wrong. 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. The file exist, but is empty. 该文件存在,但为空。 This would result in nothing being printed. 这将导致不打印任何内容。 Just check to be sure nothing has changed in the file. 只需检查以确保文件中没有任何更改。

Hint, both iostat and iomsg are available in all file I/O operations, so also for read , write , and even close . 提示, iostatiomsg在所有文件I / O操作中都可用,因此也可以进行readwrite甚至close It can't hurt to use them. 使用它们不会有伤害。

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

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