Fortran:输出大数组 - 超出长度,分成两行 [英] Fortran: output large array - exceeded length, split on two lines

查看:586
本文介绍了Fortran:输出大数组 - 超出长度,分成两行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Fortran程序中,我需要将数组写入具有特定格式的文件中。
我完全适用于较小的数组(例如下面的示例中的alen = 10),但不适用于较大的数组:然后将每行分割为两行,就好像每行超过最大字符数一样。



示例(与我的程序中的结构非常相似):

  PROGRAM output_probl 
IMPLICIT NONE

INTEGER,PARAMETER :: alen = 110
DOUBLE PRECISION,DIMENSION(alen):: a
INTEGER :: i,j

OPEN(20,file ='output.dat')
30格式(I5,1x,110(e14.6e3,1x))

DO i = 1,15
DO j = 1,alen
a(j)=(i * j ** 2)* 0.0123456789
ENDDO
write(20,30)i,(a (j),j = 1,alen)
ENDDO

END PROGRAM output_probl

它编译和运行正常(使用Compaq Visual Fortran)。只是输出文件是错误的。例如,如果我将每个数组项目的字段宽度从14更改为8,它将工作正常(这当然不是一个令人满意的解决方案)。
我想到了一个不合适的默认最大记录长度,但无法找到如何更改它(即使使用RECL,似乎不起作用 - 如果您认为它应该,欢迎使用RECL的具体示例)。



这可能是基本的,但我一直坚持它一段时间...任何帮助,欢迎,非常感谢!

解决方案

为什么不进行流访问?

  PROGRAM output_probl 
IMPLICIT NONE
$ b在连续的情况下,总会有一些处理器相关的记录长度限制。 $ b INTEGER,PARAMETER :: alen = 110
DOUBLE PRECISION,DIMENSION(alen):: a
INTEGER :: i,j

OPEN(20,file ='output .dat',access ='stream',form ='formatted',status ='replace')
30格式(I5,1x,110(e14.6e3,1x))

DO i = 1,15
DO j = 1,alen
a(j)=(i * j ** 2)* 0.0123456789
ENDDO
write(20,30)i ,(a(j),j = 1,alen)
ENDDO

END PROGRAM output_probl

需要注意的是,我会使用字符变量作为格式字符串,或者直接将它放在write语句中,而不是 FORMAT 语句



Fortran 95版本:

  PROGRAM output_probl 
IMPLICIT NONE

INTEGER,PARAMETER :: alen = 110
DOUBLE PRECISION,DIMENSION(alen):: a
INTEGER :: i,j,rl
字符(2000):: ch

inquire(iolength = rl)ch

OPEN(20,file ='output.dat',access ='direct' ,form ='unformatted',status ='replace',recl = rl)
30格式(I5,1x,110(e14.6e3,1x))

DO i = 1, (a,j)= a(j,j)=(i * j ** 2)* 0.0123456789
ENDDO
write(ch,30) ),j = 1,alen)
ch(2000:2000)= achar(10)
write(20,rec = i)ch
ENDDO

END PROGRAM output_probl


In a Fortran program, I need to write an array into a file with a specific format. I perfectly works for smaller array (e.g. alen=10 in the example below), but won't work for bigger arrays: it then splits each line into two, as if a maximum number of characters per line was exceeded.

Example (very similar to the structure in my program):

PROGRAM output_probl
IMPLICIT NONE

INTEGER, PARAMETER :: alen=110          
DOUBLE PRECISION, DIMENSION(alen)::a
INTEGER :: i,j

OPEN(20,file='output.dat')
30  format(I5,1x,110(e14.6e3,1x))

DO i=1,15
 DO j=1,alen
  a(j)=(i*j**2)*0.0123456789
 ENDDO
 write(20,30)i,(a(j),j=1,alen)
ENDDO

END PROGRAM output_probl

It compiles and runs properly (with Compaq Visual Fortran). Just the output file is wrong. If I for example change the field width per array item from 14 to 8, it'll work fine (this is of course not a satisfactory solution). I thought about an unsuitable default maximum record length, but can't find how to change it (even with RECL which doesn't seem to work - if you think it should, a concrete example with RECL is welcome).

This might be basic, but I've been stuck with it for some time... Any help is welcome, thanks a lot!

解决方案

Why not stream access? With sequential there is allways some processor dependent record length limit.

PROGRAM output_probl
IMPLICIT NONE

INTEGER, PARAMETER :: alen=110          
DOUBLE PRECISION, DIMENSION(alen)::a
INTEGER :: i,j

OPEN(20,file='output.dat',access='stream', form='formatted',status='replace')
30  format(I5,1x,110(e14.6e3,1x))

DO i=1,15
 DO j=1,alen
  a(j)=(i*j**2)*0.0123456789
 ENDDO
 write(20,30)i,(a(j),j=1,alen)
ENDDO

END PROGRAM output_probl

As a note, I would use a character variable for the format string, or place it directly in the write statement, instead of the FORMAT statement with a label.

Fortran 95 version:

PROGRAM output_probl
IMPLICIT NONE

INTEGER, PARAMETER :: alen=110          
DOUBLE PRECISION, DIMENSION(alen)::a
INTEGER :: i,j,rl
character(2000) :: ch

inquire(iolength=rl) ch

OPEN(20,file='output.dat',access='direct', form='unformatted',status='replace',recl=rl)
30  format(I5,1x,110(e14.6e3,1x))

DO i=1,15
 DO j=1,alen
  a(j)=(i*j**2)*0.0123456789
 ENDDO
 write(ch,30)i,(a(j),j=1,alen)
 ch(2000:2000) = achar(10)
 write(20,rec=i) ch
ENDDO

END PROGRAM output_probl

这篇关于Fortran:输出大数组 - 超出长度,分成两行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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