从文件到阵列的读数据 [英] Read data from file into array

查看:134
本文介绍了从文件到阵列的读数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法从文件中读取数据到一个数组,我是新来编程
 和Fortran所以任何信息将是一件幸事。

这是txt文件的程序从

阅读的一个样本

 !60
!挑战者号1.51 12712.2 1.040986E + 11
!USS科尔特斯9.14 97822.5 2.009091E + 09
!USS无畏5.76 27984.0 2.599167E + 09
!企业号航空母舰2.48 9136.3 1.478474E + 10
!USS神剑3.83 32253.0 1.286400E + 10

一起有60艘舰艇。变量之间用空格分开,
 如下

扭曲因素,光年的距离,实际速度。

这是当前code我都用过,它给了错误的模块或主
  程序阵列M在(1)必须具有固定形状

 程序engine_performance
    隐式NONE
    INTEGER ::我!循环索引
    REAL,参数:: C = 2.997925 * 10 ** 8!光年(米/秒)
    REAL,参数:: N = 60!班次
    REAL :: warpFactor!经因素
    REAL :: distanceLy!在光年的距离
    REAL :: actualTT!实际时间旅行
    REAL ::速度!在速度米/秒
    REAL :: TheoTimeT!理论上的时间旅行
    REAL ::一般!平均的发动机性能
    REAL ::位数!的发动机性能中位数
    INTEGER,DIMENSION(3,N),可分配::中号    OPEN(单位= 10,FILE =TrekTimes.txt)  我= 1,N
      READ(* 100)warpFactor,distanceLY,actualTT
         100 FORMAT(T19,F4.2,1X,F7.1,&安培;
                    1X,ES 12.6)      WRITE(*,*)M
  END DO
  CLOSE(10)
END PROGRAM engine_performance


解决方案

我第一次读你的code我错误地读 M 作为一个数组中,你的项目将船舶的文件中存储的数字。在仔细检查我认识到(a)在 M 是一个整数数组和(b)后来在code读语句读取输入文件的每一行,但不存储 warpFactor,distanceLY,actualTT 的任何地方。

制作一个野生猜测, M 应该是与各船有关的数字要素的重新presentation,这里是如何解决您的code。如果胡乱猜测是离谱的,明确你正在尝试做的,与你的code任何剩余的问题。

首先,固定的 M上的声明

  REAL,DIMENSION(:,:),可分配::中号

术语(3,N)不能在分配数组的声明中使用。因为 N 是pviously宣布为实常数它不是作为数组的程度,规范有效的$ P $。若能将数组的声明将其固定在尺寸(3,60)这意味着数组不能分配的。

所以,也会改变的声明 N

  INTEGER ::ñ

这不再是一个参数,你会从文件的第一行,这就是为什么它是摆在首位的文件中读出它的价值。

第二,我觉得你的行和列的阵列中切换。该文件具有60行(一般的),每一个都具有数字数据的3列。所以,当谈到时间来分配 M 使用语句

  ALLOCATE(M(N,3))

当然,在此之前,你不得不读 N 从文件的第一行,但是这不应该是一个严重的问题。

三,读值到数组。更改

  READ(* 100)warpFactor,distanceLY,actualTT

  READ(* 100)M(I,:)

这将读取整个行 I

最后,那些领先的 该文件中的每一行! - 摆脱他们(使用编辑器或没有他们重新创建文件)。他们prevent值的方便阅读。随着 present读 N 要求格式规范,没有它,它只是阅读(10,*)

哦,绝对最后:你应该,你有这个计划工作后,您注意的定义类型的在你最喜欢的Fortran语言教程的话题,并学习如何申报类型(飞船)用于编程的前添加pressiveness和易用性。

I am having trouble reading data from a file into an array, I am new to programing and fortran so any information would be a blessing.

this is a sample of the txt file the program is reading from

!60         
!USS Challenger    1.51 12712.2 1.040986E+11
!USS Cortez        9.14 97822.5 2.009091E+09
!USS Dauntless     5.76 27984.0 2.599167E+09
!USS Enterprise    2.48  9136.3 1.478474E+10
!USS Excalibur     3.83 32253.0 1.286400E+10

all together there is 60 ships. the variables are separated by spaces and are as follows

warp factor, distance in light years, actual velocity.

this is the current code I have used, it has given the error The module or main program array 'm' at (1) must have constant shape

PROGRAM engine_performance
    IMPLICIT NONE
    INTEGER :: i         ! loop index
    REAL,PARAMETER :: c = 2.997925*10**8 ! light years (m/s)
    REAL,PARAMETER :: n = 60 ! number of flights
    REAL :: warpFactor   ! warp factor
    REAL :: distanceLy   ! distance in light years
    REAL :: actualTT     ! actual time travel
    REAL :: velocity     ! velocity in m/s 
    REAL :: TheoTimeT    ! theoretical time travel 
    REAL :: average      ! average of engine performance
    REAL :: median       ! median of engine performance
    INTEGER, DIMENSION (3,n), ALLOCATABLE :: M



    OPEN(UNIT=10, FILE="TrekTimes.txt")

  DO i = 1,n 
      READ(*,100) warpFactor, distanceLY, actualTT
         100 FORMAT(T19,F4.2,1X,F7.1,&
                    1X,ES 12.6)

      WRITE(*,*) M 
  END DO
  CLOSE (10) 
END PROGRAM engine_performance

解决方案

The first time I read your code I mistakenly read M as an array in which your program would store the numbers from the file of ships. On closer inspection I realise (a) that M is an array of integers and (b) the read statement later in the code reads each line of the input file but doesn't store warpFactor, distanceLY, actualTT anywhere.

Making a wild guess that M ought to be the representation of the numeric factors associated with each ship, here's how to fix your code. If the wild guess is wide of the mark, clarify what you are trying to do and any remaining problems with your code.

First, fix the declaration of M

REAL, DIMENSION (:,:), ALLOCATABLE :: M

The term (3,n) can't be used in the declaration of an allocatable array. Because n is previously declared to be a real constant it's not valid as the specification of an extent of an array. If it could be the declaration of the array would fix its dimensions at (3,60) which means that the array can't be allocatable.

So, also change the declaration of n to

INTEGER :: n

It's no longer a parameter, you're going to read its value from the first line of the file, which is why it's in the file in the first place.

Second, I think you have rows and columns switched in your array. The file has 60 rows (of ships), each of which has 3 columns of numeric data. So when it comes time to allocate M use the statement

ALLOCATE(M(n,3))

Of course, prior to that you'll have had to read n from the first line in the file, but that shouldn't be a serious problem.

Third, read the values into the array. Change

  READ(*,100) warpFactor, distanceLY, actualTT

to

  READ(*,100) M(i,:)

which will read the whole of row i.

Finally, those leading ! on each line of the file -- get rid of them (use an editor or re-create the file without them). They prevent the easy reading of values. With the ! present reading n requires a format specification, without it it's just read(10,*).

Oh, and absolutely finally: you should probably, after you've got this program working, direct your attention to the topic of defined types in your favourite Fortran tutorial, and learn how to declare type(starship) for added expressiveness and ease of programming.

这篇关于从文件到阵列的读数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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