Fortran运行时错误:文件结束 [英] Getting fortran runtime error: end of file

查看:311
本文介绍了Fortran运行时错误:文件结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近学会了如何使用Fortran
中的基本文件,并且我认为它很简单:

 <$ c $ (10,*)some_variable,somevar2 
close(10)
(unit = 10,file =data.dat)
read pre>

所以我不明白为什么我写的这个函数不起作用。
它编译得很好,但是当我运行它时,它会打印:

  fortran运行时错误:文件结束

代码:

 函数Load_Names()

字符(len = 30):: Staff_Name(65)
integer :: i = 1

open(unit = 10,file =Staff_Names.txt)

while(i <65)

read(10,*)Staff_Name(i)
print *,Staff_Name( i)
i = i + 1

end do

close(10)
end Function Load_Names

p>

我使用Fortran 2008与gfortran。

解决方案

您报告错误的常见原因是程序没有找到它正在尝试打开的文件。有时你对运行时程序查找文件的目录的假设是错误的。



试试:



使用打开的语句中的 err = 选项
  • 编写要处理的代码优雅地丢失文件;正如你观察到的那样,没有这个程序崩溃;





    • 使用 inquire 语句来确定文件是否存在于您的程序正在查找的位置。


    I have recently learned how to work with basic files in Fortran and I assumed it was as simple as:

    open(unit=10,file="data.dat")
    read(10,*) some_variable, somevar2
    close(10) 
    

    So I can't understand why this function I wrote is not working. It compiles fine but when I run it it prints:

    fortran runtime error:end of file
    

    Code:

    Function Load_Names()
    
    character(len=30) :: Staff_Name(65)
    integer :: i = 1
    
    open(unit=10, file="Staff_Names.txt")
    
    do while(i < 65)
    
    read(10,*) Staff_Name(i)
    print*, Staff_Name(i)
    i = i + 1
    
    end do
    
    close(10)
    end Function Load_Names
    

    I am using Fortran 2008 with gfortran.

    解决方案

    A common reason for the error you report is that the program doesn't find the file it is trying to open. Sometimes your assumptions about the directory in which the program looks for files at run-time will be wrong.

    Try:

    • using the err= option in the open statement to write code to deal gracefully with a missing file; without this the program crashes, as you have observed;

    or

    • using the inquire statement to figure out whether the file exists where your program is looking for it.

    这篇关于Fortran运行时错误:文件结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆