041. Use g0.d and : edit descriptors with infinite repeat count to write delimited (CSV, etc.) output#

topic: Input and Output

  • g0.d will appropriately format any intrinsic type.

  • : causes processing to stop until the next I/O list item.

  • * repeats a format group as many times as necessary.

csv.f90 | | Godbolt Compiler Explorer logo | Fortran logo#
program csv
   implicit none

   integer :: x, y
   integer, parameter :: n = 3
   character(len=*), parameter :: fmt = "(*(g0.5, :, ','))"

   print fmt, "x", "y", "r"
   do x = 1, n
      do y = 1, n
         print fmt, x, y, norm2(real([x, y]))
      end do
   end do

end program csv
Output1#
x,y,r
1,1,1.4142
1,2,2.2361
1,3,3.1623
2,1,2.2361
2,2,2.8284
2,3,3.6056
3,1,3.1623
3,2,3.6056
3,3,4.2426

Note

Intrinsic function norm2 is part of the Fortran 2008 standard.



1

Compiled using GNU Fortran (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0 with no flags