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

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

问题描述

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

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

这是上面程序的输出(没有 recl):

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

推荐答案

这不是用于文件(流、顺序或直接)的 ACCESS 的问题 - 这是您使用的格式规范的结果.

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.

WRITE 语句的第二个说明符中的星号是列表定向格式 的说明.这意味着用于输出的格式基于要输出的事物列表.除此之外,还有一组用于列表定向输出的语言规则,您几乎将事物的外观留给了 Fortran 处理器(编译器).

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

可能更合适.

(从技术上讲,当打开顺序文件时,处理器定义的最大记录长度(在没有程序员指定的最大长度的情况下)不应超过.实际上,考虑到顺序格式化文件存储在磁盘上的方式几乎所有当前的 Fortran 编译器,这种技术性不会导致任何问题.)

(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天全站免登陆