FORTRAN 90如何编写长度不同的非常长的输出行 [英] FORTRAN 90 how to write very long output lines of different length

查看:234
本文介绍了FORTRAN 90如何编写长度不同的非常长的输出行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了几个小时在互联网上寻找解决这个问题的办法,但找不到任何东西。我一直在试图将未格式化的输出写入CSV输出文件,其中包含多条长度可变且数据类型多样的非常长的行。我试着先写一个长头文件,指出将要写在下面的变量,用逗号分隔。然后在下面的行中,我正在写入标题中指定的值。但是,通过顺序访问,长输出线被分成多条较短的线,这不是我所希望的。我试图在open语句中使用recl来控制行长度,但在输出后只添加了一堆乱码文本和符号,但仍然出现同样的问题。我也尝试使用直接访问,但行数不相同,所以也不起作用。我已经阅读过有关在Fortran2003中使用流I / O的问题,但我使用的是Fortran90,所以这也不起作用。我正在使用带有使用FTN95编译器的Plato IDE的Fortran 90。我在下面包含了一个类似于我想要做的示例程序,使用了一个数组和一些虚拟文本,并且在下面的输出中包含了说明问题的输出。任何人都知道我可以每写一条语句只写一行吗?任何帮助将不胜感激。

 模块类型
整数,参数:: dp = selected_real_kind(15)
结束模块类型

程序等值
使用类型
使用输入输出

隐含无

整数:: i
character(50):: fileNm
integer :: unitout2 = 20
real(dp),dimension(100):: bigArray

fileNm ='predictout2.csv '
open(unit = unitout2,file = fileNm,status =replace)

do i = 1,100
bigArray(i)= i
end do

write(unitout2,*)字,词,词,词,词,词,词,词,词,词,词,词,词,词,词,
& word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,&
& amp; word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word
write(unitout2,*)bigArray

关闭(unitout2)

结束程序

这里是输出为上面的程序(不包括recl):

  word,word,word,word,word,word,word,word,单词单词单词单词单词单词单词单词单词单词单词单词单词
单词,单词,单词,单词,单词,单词,单词,单词单词单词单词单词单词单词单词单词单词单词单词单词wo
rd单词单词单词单词
1.00000000000 2.00000000000 3.00000000000 4.00000000000
5.00000000000 6.00000000000 7.00000000000 8.00000000000
9.00000000000 10.0000000000 11.0000000000 12.0000000000
13.0000000000 14.0000000000 15.0000000000 16.0000000000 $ b $ 17.0000000000 18.0000000000 19.0000000000 20.0000000000 $ b $ 21.0000000000 22.0000000000 23.0000000000 24.0000000000
25.0000000000 26.0000000000 27.0000000000 28.0000000000
29.0000000000 30.0000000000 31.0000000000 32.0000000000 $ b $ 33.0000000000 34.0000000000 35.0000000000 36.0000000000
37.0000000000 38.0000000000 39.0000000000 40.0000000000 $ b $ 41.0000000000 42.0000000000 43.0000000000 44.0000000000 $ b $ 45.0000000000 46.0000000000 47.0000000000 48.0000000000
49.0000000000 50.0000000000 51.0000000000 52.0000000000
53.0000000000 54.0000000000 55.0000000000 56.0000000000
57.0000000000 58.0000000000 59.0000000000 60.0000000000
61.0000000000 62.0000000000 63.0000000000 64.0000000000
65.0000000000 66.0000000000 67.0000000000 68.0000000000 $ b $ 69.0000000000 70.0000000000 71.0000000000 72.0000000000 $ b 73.0000000000 74.0000000000 75.0000000000 76.0000000000 $ b $ 77.0000000000 78.0000000000 79.0000000000 80.0000000000 $ b 81.0000000000 82.0000000000 83.0000000000 84.0000000000
85.0000000000 86.0000000000 87.0000000000 88.0000000000 $ b $ 89.0000000000 90.0000000000 91.0000000000 92.0000000000 $ b $ 93.0000000000 94.0000000000 95.0000000000 96.0000000000
97.0000000000 98.0000000000 99.0000000000 100.000000000


解决方案

对于文件使用的ACCESS存在问题(流,顺序或直接) - 这是您使用的格式规范的结果。



请注意,您没有做无格式输出。格式化与未格式化的问题是输出是否为人类可读的问题。



WRITE语句的第二个说明符中的星号是列表的规范直接格式化。这意味着用于输出的格式是基于要输出的东西的列表。除了这些以及用于列表定向输出的一小部分规则,您几乎可以将事物的外观留给Fortran处理器(编译器)。

使用列表定向格式化输出,处理器可以特别允许插入尽可能多的记录,因为它看起来适合项目。为了让人们更容易阅读文件,它在这里做得相当合理。



如果您想更好地控制输出的外观,请使用一个明确的格式。例如,如下所示:

  write(unitout2,(9999(G12.5,:,',')) )bigArray 

可能更合适。

(从技术上说,当打开顺序文件时,处理器定义了最大记录长度(在没有指定最大长度的程序员的情况下),实际上,考虑到顺序格式化文件几乎全部存储在磁盘上Fortran编译器,技术性不会引起任何问题。)


I've spent hours scouring the internet for a solution to this problem and can't find anything. I have been trying to write unformatted output to a CSV output file with multiple very long lines of varying length and multiple data types. I'm trying to first write a long header that indicates the variables that will be written below, separated by commas. Then on the lines below that, I am writing the values specified in the header. However, with sequential access, the long output lines are broken into multiple shorter lines, which is not what I was hoping for. I tried controlling the line length using recl in the open statement, but that only added a bunch of garble text and symbol after the output with the same problem still occurring. I also tried using direct access but the lines are not the same length so that would not work either. I've read about using stream i/o in Fortran2003 but I'm using Fortran90, so that won't work either. I am using Fortran 90 with the Plato IDE which uses the FTN95 compiler. I included an example program similar to what I want to do below, using an array and some dummy text, and I've included the output below that illustrating the problem. Anyone know how I can just one line per write statement? Any help would be greatly appreciated.

module types
  integer, parameter :: dp=selected_real_kind(15)
end module types

program blah
  use types
  use inputoutput

  implicit none

  integer :: i
  character(50)::fileNm
  integer :: unitout2=20
  real(dp), dimension(100) :: bigArray

  fileNm='predictout2.csv'
  open(unit=unitout2,file=fileNm,status="replace")

  do i=1,100
    bigArray(i)=i
  end do

  write(unitout2,*)"word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,&
  &word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,&
  &word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word"
  write(unitout2,*)bigArray

  close(unitout2)

end program

Here's the output for the program above (without recl):

 word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word
  ,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,word,wo
  rd,word,word,word,word,word
           1.00000000000              2.00000000000              3.00000000000              4.00000000000
           5.00000000000              6.00000000000              7.00000000000              8.00000000000
           9.00000000000              10.0000000000              11.0000000000              12.0000000000
           13.0000000000              14.0000000000              15.0000000000              16.0000000000
           17.0000000000              18.0000000000              19.0000000000              20.0000000000
           21.0000000000              22.0000000000              23.0000000000              24.0000000000
           25.0000000000              26.0000000000              27.0000000000              28.0000000000
           29.0000000000              30.0000000000              31.0000000000              32.0000000000
           33.0000000000              34.0000000000              35.0000000000              36.0000000000
           37.0000000000              38.0000000000              39.0000000000              40.0000000000
           41.0000000000              42.0000000000              43.0000000000              44.0000000000
           45.0000000000              46.0000000000              47.0000000000              48.0000000000
           49.0000000000              50.0000000000              51.0000000000              52.0000000000
           53.0000000000              54.0000000000              55.0000000000              56.0000000000
           57.0000000000              58.0000000000              59.0000000000              60.0000000000
           61.0000000000              62.0000000000              63.0000000000              64.0000000000
           65.0000000000              66.0000000000              67.0000000000              68.0000000000
           69.0000000000              70.0000000000              71.0000000000              72.0000000000
           73.0000000000              74.0000000000              75.0000000000              76.0000000000
           77.0000000000              78.0000000000              79.0000000000              80.0000000000
           81.0000000000              82.0000000000              83.0000000000              84.0000000000
           85.0000000000              86.0000000000              87.0000000000              88.0000000000
           89.0000000000              90.0000000000              91.0000000000              92.0000000000
           93.0000000000              94.0000000000              95.0000000000              96.0000000000
           97.0000000000              98.0000000000              99.0000000000              100.000000000

解决方案

This isn't a problem with the ACCESS used for the file (stream, sequential or direct) - it is a consequence of the format specification that you are using.

Note that you are not doing unformatted output. Formatted versus unformatted is a question of whether the output is intended to be human readable.

The star in the second specifier of the WRITE statement is a specification of list directed formatting. This means that the format used for the output is based on the list of things to be output. Beyond that and a small set of rules in the language for list directed output, you are pretty much leaving the appearance of things up to the Fortran processor (the compiler).

With list directed formatted output the processor is specifically allowed to insert as many records as it sees fit between items. It does that here, quite reasonably, in order to make it easier for people to read the file.

If you want more control over the appearance of your output, then use an explicit format. For example, something like:

write(unitout2,"(9999(G12.5,:,','))") bigArray

might be more appropriate.

(Technically when a sequential file is opened there is a processor defined maximum record length (in the absence of a programmer specified maximum length) that should not be exceeded. Practically, given the way sequential formatted files are stored on disk by nearly all current Fortran compilers, that technicality doesn't cause any problems.)

这篇关于FORTRAN 90如何编写长度不同的非常长的输出行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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