Fortran新手,有关写入文件的问题 [英] New to Fortran, questions about writing to file

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

问题描述

我对Fortran来说是全新的,而且对编程来说很新颖。我正在尝试编写其他人编写的脚本。这给我一些问题。代码的上半部分是:

  C 
打开直接访问输出文件('JPLEPH')
C
OPEN(UNIT = 12,
。FILE ='JPLEPH',
。ACCESS ='DIRECT',
。FORM ='UNFORMATTED',
。RECL = IRECSZ,
。STATUS ='NEW')


C
C读写星历数据记录(第1070组)。
C
CALL NXTGRP(HEADER)

IF(HEADER .NE。'GROUP 1070')CALL ERRPRT(1070,'NOT HEADER')

NROUT = 0
IN = 0
OUT = 0


1 READ(*,'(2i6)')NRW,NCOEFF
if (*,'(3D26.18)',IOSTAT = IN)(DB(K),K = 1,NCOEFF)


DO WHILE((IN .EQ。0)
。.AND。(DB(2).LT。T2))

IF(2 * NCOEFF .NE。KSIZE) THEN
CALL ERRPRT(NCOEFF,'2 * NCOEFF不等于KSIZE')
ENDIF

C
C如果间隔结束时跳过此数据块比指定的开始时间少
C,或者如果它在前一个块结束时不开始
C。 (DB(2).GE.T1).AND。(DB(1).GE.DB2Z))THEN

IF(FIRST)THEN
C
C如果这是第一个适用的
C区间,请不要担心
C或重叠的区间。
C
DB2Z = DB(1)
FIRST = .FALSE。
ENDIF

IF(DB(1).NE。DB2Z)THEN
C
C当前时间间隔开始时间超过结束时间
C前一个。

CALL ERRPRT(NRW,'记录不重叠或紧靠')
ENDIF

DB2Z = DB(2)
NROUT = NROUT + 1

print *,'Out =',OUT

WRITE(12,REC = NROUT + 2,IOSTAT = OUT)(DB(K),K = 1,NCOEFF )

print *,'Out2 =',OUT


IF(OUT .NE。0)THEN
CALL ERRPRT(NROUT,
。'th record not because because of error')
ENDIF

所以,当我在屏幕上打印出Out和Out2,我发现Out = 0和Out2 = 110。由于它不再等于零,程序给了我一个错误。因此,我基本上想知道这里发生了什么:

  WRITE(12,REC = NROUT + 2,IOSTAT = OUT)( DB(K),K = 1,NCOEFF)

我假设12指的是我拥有的文件打开(并创建),并希望写入。剩下的第一个括号是做什么的?第二点的意义是什么?这是否给了我想要放在我的文件中的信息?在这种情况下,DB在哪里得到填补?



一般我想知道什么是错的?为什么OUT会改变价值? (我需要t

NCOEFF在程序开始时定义为一个整数,而DB:如DOUBLE PRECISION DB(3000),DB2Z / 0.d0 /所以我假设DB是某种类型的数组。

解决方案

引用手册 REC 表示要读取或写入的记录编号,建议参见编译器随附的文档以作进一步解释。

( DB(K),K = 1,NCOEFF)表示从1到 NCOEFF 中的所有元素。您正在查看 io-implied-do 语句。


I am completely new to Fortran and pretty new to programming in general. I am trying to compile a script someone else has written. This is giving me a few problems. The top half of the code is:

C 
C Open direct-access output file ('JPLEPH')
C 
OPEN ( UNIT = 12, 
. FILE = 'JPLEPH', 
. ACCESS = 'DIRECT', 
. FORM = 'UNFORMATTED', 
. RECL = IRECSZ, 
. STATUS = 'NEW' )


C
C Read and write the ephemeris data records (GROUP 1070). 
C 
CALL NXTGRP ( HEADER ) 

IF ( HEADER .NE. 'GROUP 1070' ) CALL ERRPRT(1070,'NOT HEADER') 

NROUT = 0 
IN = 0 
OUT = 0 


1 READ(*,'(2i6)')NRW,NCOEFF
if(NRW .EQ. 0) GO TO 1
READ (*,'(3D26.18)',IOSTAT =IN) (DB(K),K=1,NCOEFF) 


DO WHILE ( ( IN .EQ. 0 ) 
. .AND. ( DB(2) .LT. T2) ) 

IF ( 2*NCOEFF .NE. KSIZE ) THEN 
CALL ERRPRT(NCOEFF,' 2*NCOEFF not equal to KSIZE') 
ENDIF 

C
C Skip this data block if the end of the interval is less 
C than the specified start time or if the it does not begin 
C where the previous block ended. 
C 
IF ( (DB(2) .GE. T1) .AND. (DB(1) .GE. DB2Z) ) THEN 

IF ( FIRST ) THEN 
C 
C Don't worry about the intervals overlapping 
C or abutting if this is the first applicable 
C interval. 
C 
DB2Z = DB(1) 
FIRST = .FALSE. 
ENDIF 

IF (DB(1) .NE. DB2Z ) THEN 
C 
C Beginning of current interval is past the end 
C of the previous one.

CALL ERRPRT (NRW, 'Records do not overlap or abut') 
ENDIF 

DB2Z = DB(2) 
NROUT = NROUT + 1

print*,'Out =', OUT

WRITE (12,REC=NROUT+2,IOSTAT=OUT) (DB(K),K=1,NCOEFF)

print*,'Out2 =', OUT


IF ( OUT .NE. 0 ) THEN 
CALL ERRPRT (NROUT, 
. 'th record not written because of error') 
ENDIF 

So, when I print "Out" and "Out2" to the screen I find that Out=0 and Out2=110. As it is not longer equal to zero, the program gives me an error. Therefore I am basically wondering about what is happening here:

WRITE (12,REC=NROUT+2,IOSTAT=OUT) (DB(K),K=1,NCOEFF)

I assume that 12 refers to the file I have opened (and created), and want to write to. What does the rest of the first brackets do? And what is the point of the second? Does that gives me the information I want to put in my file? In case, where does DB get filled with that?

Generally I am wondering what is going wrong? Why does OUT change value? (I need t

NCOEFF is defined as an Integer in the beginning of the programme, and DB: as DOUBLE PRECISION DB(3000), DB2Z/0.d0/ , so I assume DB is an array of some sort.

解决方案

To quote the handbook REC indicates the record number to be read or written. As advised, see the documentation which accompanies your compiler for further explanation.

(DB(K),K=1,NCOEFF) means 'all the elements in DB from 1 to NCOEFF. You are looking at an io-implied-do statement.

这篇关于Fortran新手,有关写入文件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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