user2712002
user2712002

Reputation: 101

Writing on the same line in FORTRAN

In Fortran, each time one uses WRITE a new line is produced. In order to control the working of a program that is being executed, I would like to write on screen the current value of a variable, but always on the same line (erasing the previous value and starting at the beginning of the line). That is, something like

 1    CONTINUE
      "update the value of a"
      WRITE(*,*) a
      BACKSPACE "screen"
      GOTO 1

Something like WRITE(*,*,ADVANCE='NO') (incorrect anyway) is not quite what I need: this would write all the values of a one after another on a very long line.

Upvotes: 10

Views: 8753

Answers (1)

Kyle Kanos
Kyle Kanos

Reputation: 3264

A trick that I was shown for what you want is as follows

do l=1,lmax
   ...update a...
   write(*,'(1a1,<type>,$)') char(13), a
enddo

where <type> is your format specifier for a (i.e., i0 for integer).

The key is the char(13), which is the carriage return, and the $ in the format descriptor. I really don't know if there is a name for $, I just know that it works for displaying on the screen--for output to file you get an a on each line.

Upvotes: 9

Related Questions