Fortran 90的读入从数据文件字符与整数 [英] Fortran 90 Reading in Characters and Integers from a data file

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

问题描述

好了,在我的计划,我应该采取从比打开并阅读内容的用户的数据文件的名称。但是,当我打开并阅读所有的文字只是最终被******,所有的整数最终被0 IDK如果这是我如何的文件或文件格式阅读?

该文件将包含这样的事情:(其中城市数量是第一个数字)

  4
圣地亚哥
0
350
900
1100
凤凰
350
0
560
604
丹佛
900
560
0
389
达拉斯
1100
604
389
0

到目前为止,我的code这是第一次在那里我参加的第一个数字不是每个firstnumber * I + I传递是应该进入字符数组的城市。现在我存储到一个整数数组,但真的希望它变成一个整数矩阵称为d_table但我不能想办法号码的其余部分做立即读。

 程序P4隐式NONEINTEGER ::号,状态,I,J,K,置换= 0,距离= 0,距离= 999999
CHARACTER(50)::文件名!文件名超过50个被截断
CHARACTER(20),外形尺寸(10)::城市
INTEGER,DIMENSION(100)::温度
INTEGER,DIMENSION(10,10):: d_table
INTEGER,DIMENSION(10)::路径,best_pathWRITE(*,'(1X,A)',ADVANCE =NO)中输入文件名:
*阅读,文件名!打开我们创建的文件,读取其中的内容OPEN(单位= 15,FILE =文件名,STATUS =老字号,ACTION =READ,&安培;
     IOSTAT =状态)
IF(状态/ = 0),那么
    PRINT *的错误,无法打开文件进行读取。
    停
万一READ(单位= 15,FMT = 100,IOSTAT =状态)号
J = 0
K = 0
DO I = 0,号码*号
    IF(我==Ĵ*号+ J)THEN
        READ(单位= 15,FMT = 200,IOSTAT =状态)市(J)
        当J = J + 1个
    其他
        READ(单位= 15,FMT = 100,IOSTAT =状态),温度(K)
        K = K + 1
    万一
END DO
K = 0
DO I = 0,号码
    DO J = 0时,数
        d_table(I,J)=温度(K)的
        K = K + 1
    END DO
END DO
100 FORMAT(I6)
200 FORMAT(A)
END PROGRAM P4


解决方案

这行

  DO I = 0,号码*号

看起来靠不住的给我;该循环将执行17次。当然,你想读数量 5行组,每组是一个城市的名字后面四个整数?这将是一个小循环嵌套一个很好的案例,像

 做九= 1,号码
   阅读(15,*)城市(九)
   做JX = 1,4
      阅读(15,*)d_table(IX,JX)
   做到底
做到底

给定这样一个简单的输入文件格式,没有必要与报表格式打扰,列表控制的输入会工作得很好。

我看不到code是做所有的指数算法的点,也许我错过了一些东西。

Ok, so In my program I'm supposed to take in the name of the data file from the user than open it and read the contents. But when I open and read it all that characters just end up being ****** and all the integers end up being 0. IDK if it's how I'm reading in the file or the format?

The file will contain something like this: (where the number of cities is the first number)

4 
SanDiego 
0 
350 
900 
1100 
Phoenix 
350 
0 
560 
604 
Denver 
900 
560 
0 
389 
Dallas 
1100 
604 
389 
0 

So far my code is this where first I take in the first number than on every firstnumber * I + I pass is supposed to go into the character array city. Now the rest of the numbers I am storing into a integer array, but really want it into a integer matrix called d_table but I couldn't think of a way to do that immediately on the read.

PROGRAM p4

IMPLICIT NONE

INTEGER :: number, status, I, J, K, permutation = 0, distance = 0, distance = 999999
CHARACTER(50) :: filename  ! Filenames longer than 50 are truncated
CHARACTER(20), DIMENSION(10) :: city
INTEGER, DIMENSION(100) :: temp
INTEGER, DIMENSION(10,10) :: d_table
INTEGER, DIMENSION(10) :: path, best_path

WRITE (*, '(1x,A)', ADVANCE="NO") "Enter filename:  "
READ *, filename

! Open the file we created and read the contents

OPEN(UNIT=15, FILE=filename, STATUS="OLD", ACTION="READ",&
     IOSTAT=status)
IF(status /= 0) THEN
    PRINT *, "ERROR, could not open file for reading."
    STOP
END IF

READ (UNIT=15, FMT = 100, IOSTAT=status) number
J = 0
K = 0
DO I = 0, number*number
    IF(I == J*number+J) THEN
        READ (UNIT=15, FMT = 200, IOSTAT=status) city(J)
        J = J + 1
    ELSE
        READ (UNIT=15, FMT = 100, IOSTAT=status) temp(K)
        K = K + 1
    END IF
END DO
K = 0
DO I = 0, number
    DO J = 0, number
        d_table(I,J) = temp(K)
        K = K + 1
    END DO
END DO
100 FORMAT(I6)
200 FORMAT (A)
END PROGRAM p4

解决方案

This line

DO I = 0, number*number

looks wonky to me; the loop will be executed 17 times. Surely you want to read number groups of 5 lines, each group being one city name followed by four integers ? That would be a good case for a little loop nest, something like

do ix = 1, number
   read(15,*) city(ix)
   do jx = 1, 4
      read(15,*) d_table(ix,jx)
   end do
end do

Given such a simple input file format there's no need to bother with format statements, list-directed input will work just fine.

I can't see the point of all the index arithmetic the code is doing, perhaps I've missed something.

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

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