在数组中写入字符和数字 [英] Writing both characters and digits in an array

查看:321
本文介绍了在数组中写入字符和数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Fortran代码,它读取带有单独字符和数字行的txt文件,然后将它们写入一个包含20个元素的1D数组中。
此代码与Fortran 77编译器Force 2.0.9不兼容。我的问题是,我们如何使用Fortran 77编译器来应用合并过程;即定义一维数组,然后将txt文件逐行写入数组元素中?



txt文件如下:

案例1:

  10 0 1 2 0 

1.104 1.008 0.6 5.0

25 125.0 175.0 0.7 1000.0

0.60

1 5

高级案例

  15 53 0 10 0 1 0 0 1 0 0 0 0 

0 0 0 0

0 0 1500.0 0 0 .03

0 0.001 0

0.1 0 0.125 0.08 0.46

0.1 5.0 0.04

@ Jason:
我是初学者,仍在学习Fortran。我猜Force 2使用g77。
以下是原始代码的相应部分。因此,Force 2编辑器会返回空的txt文件。

  DIMENSION CARD(20)
字符* 64 FILENAME
DATA xHEND / 4HEND /

OPEN(UNIT = 3,FILE ='CON')
OPEN(UNIT = 4,FILE ='CON')
OPEN UNIT = 7,STATUS ='SCRATCH')

WRITE(3,9000)'请输入输入文件名称:'
9000格式(A)
读取(4, 9000)FILENAME

OPEN(单位= 5,FILE = FILENAME,STATUS ='OLD')
WRITE(3,9000)'请输入输出文件名称:'
READ(4,9000)FILENAME
OPEN(UNIT = 6,FILE = FILENAME,STATUS ='NEW')
FILENAME ='...'

IR = 7
IW = 6
IP = 15
5 REWIND IR
I = 0
2 READ(5,7204,END = 10000)CARD
IF(I .EQ。0)WRITE(IW,7000)
7000 FORMAT(1H1 / 10X,15HINPUT DECK ECHO / 10X,15(1H-))
I = I + 1
WRITE ,9204)I,CARD
IF(CARD(1).EQ。XHEND)前往7020
写入(IR,7204)卡
7204格式(20A4)
9204格式(1X,I4,2X ,20A4)
转到2
7020 REWIND IR


解决方案

看起来,CARD被用作一个可容纳20个4字符的字符串。我没有看到声明是一个字符变量,只是作为一个数组,所以也许在极其古老的FORTRAN风格中使用非字符变量来保存字符?您使用的是20A4格式,因此必须将这些值精确定位在20个4个字符的组中。您必须添加空格,以便它们对齐到4列的组中。



如果要读取数字,将它们读入数字类型会更容易,并且使用列表控制的IO:

 实际值(20)
读取(5,*)值

然后您不必担心文件中值的精确定位。



这真是一个古老的FORTRAN ......甚至是FOR-FORTRAN-77的风格。我不记得上次看到Hollerith(H)格式的时候了!编辑:虽然我很喜欢Fortran的许多编程任务,但我不会使用FORTRAN 66!电脑应该让事情变得更简单......没有理由必须数字。而不是

  7000 FORMAT(1H1 / 10X,15HINPUT DECK ECHO / 10X,15(1H-))

您可以使用

  7000 FORMAT(/ 10X,INPUT DECK ECHO/ 10X,15( - ))

我可以考虑使用Hollerith代码的两个原因:不要更改旧版源代码(当前的Fortran编译器可以处理30年前已过时的功能!Fortran源代码永远不会死!)或研究计算语言的历史。这个名字是一个伟大的计算先驱者的名字,他的发明在一年内完成了1890年美国人口普查,1880年人口普查历时8年: http://en.wikipedia.org/wiki/Herman_Hollerith



我很怀疑你会在第一列看到1今天进行马车控制。我不得不查看1是页面弹出的代码。你更有可能在你的输出中看到它。请参阅 Fortran控制字符(托架控件)是否仍然在编译器?


I have a Fortran code which reads a txt file with seperate lines of characters and digits and then write them in a 1D array with 20 elements. This code is not compatible with Fortran 77 compiler Force 2.0.9. My question is that how we can apply the aformenetioned procedure using a Fortran 77 compiler;i.e defining a 1D array nd then write the txt file line by line into elements of the array?

Thank you in advance.

The txt file follows:

Case 1:

10  0  1  2  0

1.104  1.008  0.6   5.0

25  125.0  175.0  0.7  1000.0

0.60

1  5

Advanced Case

15 53 0 10 0 1 0 0 1 0 0 0 0

0 0 0 0

0 0 1500.0 0 0 .03

0 0.001 0

0.1 0 0.125 0.08 0.46

0.1 5.0 0.04

@ Jason: I am a beginner and still learning Fortran. I guess Force 2 uses g77. The followings are the correspond part of the original code. Force 2 editor returns an empty txt file as a result.

       DIMENSION CARD(20)
       CHARACTER*64 FILENAME
       DATA XHEND / 4HEND  /

       OPEN(UNIT=3,FILE='CON')
       OPEN(UNIT=4,FILE='CON')
       OPEN(UNIT=7,STATUS='SCRATCH')

       WRITE(3,9000) 'PLEASE ENTER THE INPUT FILE NAME : '
  9000 FORMAT (A)
       READ(4,9000) FILENAME

       OPEN(UNIT=5,FILE=FILENAME,STATUS='OLD')
       WRITE(3,9000) 'PLEASE ENTER THE OUTPUT FILE NAME : '
       READ(4,9000) FILENAME
       OPEN(UNIT=6,FILE=FILENAME,STATUS='NEW')
       FILENAME = '...'

       IR = 7
       IW = 6
       IP = 15
  5    REWIND IR
       I = 0
  2    READ (5,7204,END=10000) CARD
       IF (I .EQ. 0 ) WRITE (IW,7000)
  7000 FORMAT (1H1 / 10X,15HINPUT DECK ECHO / 10X,15(1H-))
       I= I + 1
       WRITE (IW,9204)  I,CARD
       IF (CARD(1) .EQ. XHEND ) GO TO 7020
       WRITE (IR,7204) CARD
  7204 FORMAT (20A4)
  9204 FORMAT (1X,I4,2X,20A4)
       GO TO 2
  7020 REWIND IR         

解决方案

It looks that CARD is being used as a to hold 20 4-character strings. I don't see the declaration as a character variable, only as an array, so perhaps in extremely old FORTRAN style a non-character variable is being used to hold characters? You are using a 20A4 format, so the values have to be positioned in the file precisely as 20 groups of 4 characters. You have to add blanks so that they are aligned into groups of 4 columns.

If you want to read numbers it would be much easier to read them into a numeric type and use list-directed IO:

real values (20)
read (5, *) values

Then you wouldn't have to worry about precision positioning of the values in the file.

This is really archaic FORTRAN ... even pre-FORTRAN-77 in style. I can't remember the last time that I saw Hollerith (H) formats! Where are you learning this from?

Edit: While I like Fortran for many programming tasks, I wouldn't use FORTRAN 66! Computers are supposed to make things easier ... there is no reason to have to count characters. Instead of

7000 FORMAT (1H1 / 10X,15HINPUT DECK ECHO / 10X,15(1H-))

You can use

7000 FORMAT ( / 10X, "INPUT DECK ECHO" / 10X, 15("-") )

I can think of only two reasons to use a Hollerith code: not bothering to change legacy source code (it is remarkable that a current Fortran compiler can process a feature that was obsolete 30 years ago! Fortran source code never dies!), or studying the history of computing languages. The name honors a great computing pioneer, whose invention accomplished the 1890 US Census in one year, when the 1880 Census took eight years: http://en.wikipedia.org/wiki/Herman_Hollerith

I much doubt that you will see the "1" in the first column performing "carriage control" today. I had to look up that "1" was the code for page eject. You are much more likely to see it in your output. See Are Fortran control characters (carriage control) still implemented in compilers?

这篇关于在数组中写入字符和数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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