Writing to file with no leading spaces at the Start of Line and No Blank End Line

56 Views Asked by At

Hi All Fortran Lovers,

I am trying to write to a file which outputs three variables as

      program main

      integer N, u
      parameter(u=20)

      open (u, FILE='points.dat', STATUS='new')
      do 10 i= 1, 100 
          write(u,100) i, i*2, i*5
 10   continue 
 100  format (I5, I10, 9X, I10)
      close(u)

      print *,'COMPLETE!!'
      end

Which Gives output (points.dat stripped file content):

       1         2                  5
       2         4                 10
       3         6                 15
       4         8                 20
       5        10                 25
       6        12                 30
       7        14                 35
       8        16                 40
       9        18                 45
      10        20                 50
      11        22                 55
       12        24                 60
      ...
      ...
      ...
      ...
      ...
      99       198                495
     100       200                500
   |(This line added by the write statement)

But I want something like this:

1         2                  5
2         4                 10
3         6                 15
4         8                 20
5        10                 25
6        12                 30
7        14                 35
8        16                 40
9        18                 45
10       20                 50
11       22                 55
12       24                 60
  ...
  ...
  ...
  ...
  ...
  99       198                495
 100       200                500|(The cursor stop here)

i.e. No space at start of each line. The last line stops after printing '500'

I tried using Horizontal spacing using '1X' specifier but no success.

1

There are 1 best solutions below

1
AudioBubble On

Add advance='no' in write statement. If the line is not the last one, write EOL:

      do 10 i= 1, 100 
          write(u,100,advance='no') i, i*2, i*5
          if (i.ne.100) write(u,*)
10   continue 

Edit: I see it now, it seems that the fortran program will add EOL to the end of file anyway. Then you have to use external programs to truncate your file, see for example https://www.quora.com/How-do-I-chop-off-just-the-last-byte-of-a-file-in-Bash .