读取FORTRAN中的输入文件 [英] Reading input files in FORTRAN

查看:140
本文介绍了读取FORTRAN中的输入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目的:创建一个程序,它接受两个单独的文件,打开并读取它们,将其内容分配给数组,与这些数组做一些数学运算,创建一个包含产品编号的新数组,并打印到一个新文件。够简单吧?

我的输入文件在开头处有注释字符。一个问题是,它们是'#',它们是大多数绘图程序的注释字符,但不是FORTRAN。告诉计算机不要看这些角色的简单方法是什么?由于我没有以前的FORTRAN经验,我正在用两个测试文件来解决这个问题。这是我到目前为止:

 程序收益
隐式无
实数,维数(1: 4,1:8):: X,Y,Z
OPEN(1,FILE ='test.out',&
STATUS ='OLD',ACTION ='READ')!打开第一个文件
READ(1,*),X
OPEN(2,FILE ='test2.out',&
STATUS ='OLD',ACTION ='READ') ! (2,*),Y
PRINT *,X,Y

Z = X * Y
! PRINT *,Z
OPEN(3,FILE ='test3.out',STATUS ='NEW',ACTION ='WRITE')!创建一个新文件
WRITE(3,*),Z
CLOSE(1)
CLOSE(2)
CLOSE(3)
结束程序

PS。请不要用一堆代码猴子gobblety gook压倒我。我是一个总编程新手。我不明白所有的术语,这就是我来到这里的原因,而不是在现有网站上寻求帮助。谢谢。

解决方案

编写一个子程序,将该逻辑放入一个位置,以便您可以为两个文件调用该逻辑。您需要将每行读取为一个字符串,并添加一个IF测试以检查给定行是否以#开头。如果行以#开始,请阅读下一行。如果没有,请将字符串转换为值并将其添加到要返回的值的数组中。


Purpose: Create a program that takes two separate files, opens and reads them, assigns their contents to arrays, do some math with those arrays, create a new array with product numbers, print to a new file. Simple enough right?

My input files have comment characters at the beginning. One trouble is, they are '#' which are comment characters for most plotting programs, but not FORTRAN. What is a simple way to tell the computer not to look at these characters? Since I have no previous FORTRAN experience, I am plowing through this with two test files. Here is what I have so far:

PROGRAM gain
  IMPLICIT NONE
  REAL, DIMENSION (1:4, 1:8)     :: X, Y, Z
  OPEN(1, FILE='test.out', &
        STATUS='OLD', ACTION='READ')            ! opens the first file
  READ(1,*), X
  OPEN(2, FILE='test2.out', &
    STATUS='OLD', ACTION='READ')            ! opens the second file
  READ(2,*), Y
  PRINT*, X, Y

  Z = X*Y
!  PRINT*, Z
  OPEN(3, FILE='test3.out', STATUS='NEW', ACTION='WRITE')   !creates a new file
  WRITE(3,*), Z
  CLOSE(1)
  CLOSE(2)
  CLOSE(3)
END PROGRAM

PS. Please do not overwhelm me with a bunch of code monkey gobblety gook. I am a total programming novice. I do not understand all the lingo, that is why I came here instead of searching for help in existing websites. Thanks.

解决方案

Write a subroutine that puts this logic into one spot for you so you can call it for both files. You'll need to read each line as a string and add an IF test to check whether a given line starts with a "#" or not. If the line starts with a "#", just read the next line. If not, convert the string to a value and add it to the array of values you're returning.

这篇关于读取FORTRAN中的输入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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