Search code examples
fortrangfortranfortran90fortran95

How to remove the empty space in the output columns in Fortran?


I am writing some data in a text file using Fortran code. The output data I want to have should look like the following.

ITEM: ATOMS id type xs ys zs
1 1 0.004775 0.074550 0.697303
2 1 0.017240 0.107659 0.721766
3 1 0.015886 0.123553 0.652037
.
.
.
10 1 0.014177 0.208595 0.748898
11 1 0.036628 0.185849 0.723589
12 1 0.055109 0.218853 0.706833
.
.
.
100 1 0.854597 0.087217 0.713019
101 1 0.861486 0.076430 0.637017
102 1 0.900569 0.109375 0.747426

To print the output data as shown before I am using the following code.

write(21,'(g0)') "ITEM: ATOMS id type xs ys zs"
write(21,'(i3,i2,3f9.6)') ll, i_type, r(:,i)

However, the output from this code I am getting is shown below. As you can see there are blank spaces at the beginning of the columns.

ITEM: ATOMS id type xs ys zs
  1 1 0.004775 0.074550 0.697303
  2 1 0.017240 0.107659 0.721766
  3 1 0.015886 0.123553 0.652037
.
.
.
 10 1 0.014177 0.208595 0.748898
 11 1 0.036628 0.185849 0.723589
 12 1 0.055109 0.218853 0.706833
.
.
.
100 1 0.854597 0.087217 0.713019
101 1 0.861486 0.076430 0.637017
102 1 0.900569 0.109375 0.747426

How do I eliminate these blank spaces at the beginning and the output maybe looks more left-justified?


Solution

  • The i3 makes the first number to always use three columns (characters). To use only the necessary number of columns use i0 or the generic g0.