在 Fortran 中逐行读取逗号分隔的文本文件
我是一名 Fortran 新手。我希望能够读取文本文件并将其内容保存在各个变量中。我发现了一个非常有用的 Fortran 教程 (http://www.math .hawaii.edu/~hile/fortran/fort7.htm#read),我正在尝试遵循那里列出的示例之一。具体来说,我制作了一个名为 data.txt 的文本文件,其中包含以下文本:
1.23, 4.56, 7.89
11, 13, "Sally"
我已将此文本文件保存在当前目录中。然后,我创建了一个文件 test.f90 (也将其保存在我的当前目录中),其中包含以下代码:
PROGRAM test
IMPLICIT NONE
REAL :: x, y, z
INTEGER :: m, n
CHARACTER first*20
OPEN(UNIT = 7, FILE = "data.txt")
READ(7,*) x, y, z
READ(7,*) m, n, first
PRINT *, x
PRINT *, y
PRINT *, z
PRINT *, m
PRINT *, n
PRINT *, first
END PROGRAM test
我正在使用 GNU Fortran 编译器,我认为它至少包含以下功能:包括 Fortran95。上面的代码似乎可以编译,至少在默认设置下)。但是,当我运行生成的可执行文件时,收到以下错误消息:
At line 10 of file test.f90 (unit = 7, file = 'data.txt')
Fortran runtime error: End of file
第 10 行是 READ(7,*) m, n, first 行。你能帮我看看我在上面的代码中做错了什么吗?
I am a Fortran novice. I would like to be able to read a text file and save its contents in individual variables. I found a very helpful Fortran tutorial (http://www.math.hawaii.edu/~hile/fortran/fort7.htm#read), and I am trying to follow one of the examples listed there. Specifically, I made a text file called data.txt with the following text:
1.23, 4.56, 7.89
11, 13, "Sally"
I have saved this text file in my current directory. Then, I have created a file test.f90 (also saving it in my current directory) containing the following code:
PROGRAM test
IMPLICIT NONE
REAL :: x, y, z
INTEGER :: m, n
CHARACTER first*20
OPEN(UNIT = 7, FILE = "data.txt")
READ(7,*) x, y, z
READ(7,*) m, n, first
PRINT *, x
PRINT *, y
PRINT *, z
PRINT *, m
PRINT *, n
PRINT *, first
END PROGRAM test
I am using the GNU Fortran compiler, which I think includes the features at least up to and including Fortran95. The above code appears to compile okay, at least with the default settings). But when I run the resulting executable, I get this error message:
At line 10 of file test.f90 (unit = 7, file = 'data.txt')
Fortran runtime error: End of file
Line 10 is the line READ(7,*) m, n, first. Can you please help me see what I am doing wrong in the above code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我可以重现您的确切错误消息和正确的输出。我在 Windows 上使用 gfortran,并使用记事本创建数据文件。
如果使用行结束符(通过按 Enter 键)终止第二个数据行,程序将显示正确的输出;如果不终止它,它将在执行过程中显示错误。
基本上,运行时尝试读取一行,但在到达行尾之前遇到文件结束字符。
I can reproduce both your exact error message and the correct output. I'm using gfortran on Windows, and Notepad to create the data file.
If you terminate the second data line with an end-of-line character (by hitting the Enter key), the program will show the correct output; if you don't terminate it, it will display the error during execution.
Basically, the runtime tries to read a line, but encounters an end-of-file character before it reaches the end of the line.
当我使用您的示例程序和示例数据时,它起作用了!恭喜!输出是:
为了猜测它不适合您的可能原因,有时 Fortran 可执行文件可能对行结尾敏感,要求操作系统使用正确的行终止符,包括数据文件的最后一行。相反,许多编辑器会默默地转换行结尾。我经常在使用 Microsoft 程序编写的文件时遇到此问题。
When I your sample program, with your sample data, it worked! Congratulations! Output was:
To guess a possible reason that it isn't working for you, sometimes Fortran executables can be sensitive to line endings, demanding the correct line-terminator for the OS, including on the last line of a data file. Conversely many editors will silently convert line endings. I commonly run into this problem with files written by Microsoft programs.
对于某些编译器来说,在最后一行数据之后添加新行非常重要。例如,gfortran 是一个需要它的编译器,并且它是非常合乎逻辑的。 Sun (Oracle) 编译器不需要这个。
For some compilers it is important to add a new line after the last line with the data. For example gfortran is a compiler which needs that and it is quite logical. The Sun (Oracle) compiler doesn't need that.