views:

3032

answers:

4

I want to display the progress of a calculation done with a DO-loop, on the console screen. I can print out the progress variable to the terminal like this:

PROGRAM TextOverWrite_WithLoop
IMPLICIT NONE
INTEGER :: Number, Maximum = 10
 DO Number = 1, MAXIMUM   
  WRITE(*, 100, ADVANCE='NO') REAL(Number)/REAL(Maximum)*100     
  100 FORMAT(TL10, F10.2)
  ! Calcultations on Number     
 END DO    
END PROGRAM TextOverWrite_WithLoop

The output of the above code on the console screen is:

10.00 20.00 30.00 40.00 50.00 60.00 70.00 80.00 90.00 100.00

All on the same line, wrapped only by the console window.

The ADVANCE='No' argument and the TL10 (tab left so many spaces) edit descriptor works well to overwrite text on the same line, e.g. the output of the following code:

WRITE(*, 100, ADVANCE='NO') 100, 500
100 FORMAT(I3, 1X, TL4, I3)

Is:

500

Instead of:

100 500

Because of the TL4 edit descriptor.

From these two instances one can conclude that the WRITE statement cannot overwrite what has been written by another WRITE statement or by a previous execution of the same WRITE satement (as in a DO-loop).

Can this be overcome somehow?

I am using the FTN95 compiler on Windows 7 RC1. (The setup program of the G95 compiler bluescreens Windows 7 RC1, even thought it works fine on Vista.)

I know about the question Supressing line breaks in Fortran 95 write statements, but it does not work for me, because the answer to that question means new ouput is added to the previous output on the same line; instead of new output overwriting the previous output.

Thanks in advance.

+1  A: 

Diclaimer, I know nothing about Fortran.

But in Console output, the old trick is to print a CR without a LF. So if you can print (use) ASCII codes you could try adding a CR (ASCII 13 or 0x0D).

Henk Holterman
+4  A: 

There is no solution to this question within the scope of the Fortran standards. However, if your compiler understand backslash in Fortran strings (GNU Fortran does if you use the option -fbackslash), you can write

  write (*,"(A)",advance="no") "foo"
  call sleep(1)
  write (*,"(A)",advance="no") "\b\b\bbar"
  call sleep(1)
  write (*,"(A)",advance="no") "\b\b\bgee"
  call sleep(1)
  write (*,*)
  end

This uses the backslash character (\b) to erase previously written characters on that line.

NB: if your compiler does not understand advance="no", you can use related non-standard tricks, such as using the $ specifier in the format string.

FX
+1  A: 

Thanks for this posting. The following should be portable across systems by use of ACHAR(13) to encode the carriage return. cheers Tony

character*1 creturn ! CODE:: creturn = achar(13) ! generate carriage return ! other code ... WRITE( * , 101 , ADVANCE='NO' ) creturn , i , npoint 101 FORMAT( a , 'Point number : ',i7,' out of a total of ',i7)

tony rollett
You should use the [code /] tags (or indenting) to syntax highlight your code and make it more readable. :)
Nathan Taylor
+1  A: 

OPEN(6,CARRIAGECONTROL ='FORTRAN')
DO I=1,5
WRITE(6,'(1H+" ",I)') I
ENDDO
THIS PROBABLY WORKS

AMK