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

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

问题描述

我试图了解READ语句在Fortran中的工作原理。为此,我编写了以下简单程序:

 程序主
整数a,b,c

open(unit = 10,file ='test.txt',status ='old')
read(10,*)a,b,c
print *,a ,b,c
关闭(10)
最终计划主要

我跑这在终端上(使用Mac):gfortran Main.f95;打开a.out

我得到以下错误:在Main.f95文件的第5行(unit = 10,file ='test.txt')
Fortran运行时错误:文件结束



我已经在网络上寻找解决方案,并提出在READ说明符中添加IOSTAT = ...的建议,因为如下:

 程序主
整数a,b,c,IOstatus

open单元= 10,file ='test.txt',status ='old')
读(10,*,IOstat = IOstatus)a,b,c
print *,a,b,c, IOstatus
close(10)
end program main

当我这样做时,该程序运行成功。但是,打印命令显示2 0 1 -1,这是错误的,因为test.txt文件包含1,2,3。



我已经试着在这里和那里调整一些东西,但没有好处。我试图做一些(我认为)应该很简单:从.txt文件中读取整数列表。任何关于我在做什么错误的帮助都将非常感激。

我认为你的问题与 text.txt 文件而不是Fortran代码。尝试在其中添加和结束行字符(输入)。



请参阅这个相关帖子的更多细节。


I'm trying to understand how the READ statement works in Fortran. To do this, I've written the following simple program:

program main 
integer a,b,c

open(unit=10,file='test.txt',status='old')
read(10,*)a,b,c
print*,a,b,c
close(10)
end program main

I run this on the terminal (using a Mac): gfortran Main.f95; open a.out

I get the following error: At line 5 of file Main.f95 (unit = 10, file = 'test.txt') Fortran runtime error: End of file

I've looked for solutions all around the web and came upon the suggestion of adding IOSTAT=... inside the READ specifiers, as follows:

program main 
integer a,b,c,IOstatus

open(unit=10,file='test.txt',status='old')
read(10,*,IOstat=IOstatus)a,b,c
print*,a,b,c,IOstatus
close(10)
end program main

When I do this, the program runs successfully. However, the print command displays "2 0 1 -1", which is erroneous as the test.txt file contains "1,2,3".

I've tried tweaking things here and there, but no good. I'm trying to do something that (I think) should be very simple: reading a list of integers from a .txt file. Any help as to what I'm doing wrong would be GREATLY appreciated.

解决方案

I think that your problem is related with the text.txt file rather than the Fortran code. Try to add and end-of-line character (enter) in it. I have successfully run your original code with gfortran 5.4.

See this related post for more details.

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

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