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

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

问题描述

目的:创建一个程序,该程序需要两个单独的文件,打开并读取它们,将它们的内容分配给数组,对这些数组进行一些数学运算,创建一个带有产品编号的新数组,打印到一个新文件.够简单吧?

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?

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

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.请不要用一堆代码猴子 gobblety gook 压倒我.我是一个完全的编程新手.我不懂所有的术语,这就是为什么我来这里而不是在现有网站上寻求帮助的原因.谢谢.

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.

推荐答案

编写一个子例程,将这个逻辑放在一个位置,以便您可以为两个文件调用它.您需要将每一行读取为一个字符串并添加一个 IF 测试以检查给定行是否以#"开头.如果该行以#"开头,则只需阅读下一行.如果不是,则将字符串转换为一个值并将其添加到您要返回的值数组中.

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天全站免登陆