简体   繁体   English

在Fortran77中打开/读取.dat文件

[英]Opening/reading .dat file in Fortran77

I'm trying to read through a set of points in a .dat file and to run the points through an algorithm in my .f file. 我正在尝试通读.dat文件中的一组点,并通过我的.f文件中的算法来运行这些点。 I've been working with the OPEN statement: 我一直在使用OPEN语句:

OPEN(UNIT=1,FILE='POINTS.DAT',FORM='UNFORMATTED')

and for a starter, have been trying to print out the values in the .dat - not working. 对于初学者,一直在尝试打印.dat中的值-不起作用。

PRINT *, 1

PRINT *, POINTS.DAT

PRINT *, 'POINTS.DAT'

Should I ditch trying to print out the values? 我应该放弃尝试打印出值吗? How should I be indexing the values in the .dat? 我应该如何索引.dat中的值? Should I do a DO loop and have it loop n times through my the number of data points in my file? 我是否应该执行DO循环并使它循环遍历文件中的数据点数? How do I call those values? 我该如何称呼这些价值观?

After the open you have to read the values from the file. 打开后,您必须从文件中读取值。 You can't print them by using the file name in a print statement. 您不能通过在打印语句中使用文件名来打印它们。 If you are correct that is is a binary / unformatted file, you use a read without a format: read (1) item . 如果正确的是二进制/未格式化的文件,则使用不带格式的read (1) itemread (1) item You can either use a do loop and read one item at a time or read all the items into an array. 您可以使用do循环并一次读取一个项目,也可以将所有项目读取到一个数组中。 If the file was written with another Fortran program, you should use the same method (single item or array) as was used for writing because the file will be record based. 如果文件是用另一个Fortran程序编写的,则应使用与写入相同的方法(单个项目或数组),因为该文件将基于记录。 If the file was written with another language, add access='stream' to your open statement to inform Fortran that it doesn't have the record structure normally used by Fortran. 如果文件是用另一种语言编写 ,请在打开的语句中添加access ='stream' ,以告知Fortran它不具有Fortran通常使用的记录结构。

Basic structure 1: 基本结构1:

real :: item  ! or whatever type
open (unit=1, ...
ReadLoop: do
  read (1, end=99) item
  process item...
end do ReadLoop
99 continue

Basic structure 2: 基本结构2:

real, dimension (NUMBER) :: array
open (unit=1,..
read (1) array
process array...

This is basic Fortran ... you will probably make faster progress if you find a book to learn from. 这是基本的Fortran ...如果您找到可以学习的书,则可能会取得更快的进步。 I like Fortran 90/95 Explained by Metcalf and Reid. 我喜欢Metcalf和Reid 解释Fortran 90/95

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

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