I also recommend the csv_file module from FLIBS . Fortran is well prepared for reading csv files, but not so much for writing them. With csv_file module you put
use csv_file
at the beginning of your function / subroutine, and then call it with:
call csv_write(unit, value, advance)
where unit = file number, value = array or scalar value you want to write, and advance = .true. or .false. depending on whether you want to go to the next line or not.
Program Example:
program write_csv use csv_file implicit none integer :: a(3), b(2) open(unit=1,file='test.txt',status='unknown') a = (/1,2,3/) b = (/4,5/) call csv_write(1,a,.true.) call csv_write(1,b,.true.) end program
exit:
1,2,3
4,5
If you just want to use the write command, I think you should do it like this:
write(1,'(I1,A,I1,A,I1)') a(1),',',a(2),',',a(3) write(1,'(I1,A,I1)') b(1),',',b(2)
which is very confusing and requires that you know the maximum number of digits your values ββwill have.
I highly recommend using the csv_file module. This certainly saved me from many disappointments.