获取 fortran 运行时错误:文件结尾 [英] Getting fortran runtime error: end of file

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

问题描述

我最近学习了如何在 Fortran 中处理基本文件我认为它很简单:

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

代码:

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

我正在使用 Fortran 2008 和 gfortran.

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.

试试:

  • open 语句中使用 err= 选项来编写代码以优雅地处理丢失的文件;如您所见,如果没有这个,程序就会崩溃;
  • 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;

  • 使用 inquire 语句确定文件是否存在于您的程序正在查找的位置.
  • using the inquire statement to figure out whether the file exists where your program is looking for it.

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

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