Fortran 90 how to write very long output lines of

2020-02-13 02:12发布

问题:

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

回答1:

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.)