How do I format a PRINT or WRITE statement to overwrite the current line on the console screen?

后端 未结 4 739
借酒劲吻你
借酒劲吻你 2021-01-05 05:04

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:

         


        
相关标签:
4条回答
  • 2021-01-05 05:38

    The following worked perfectly using g95 fortran:

          NF = NF + 1
          IF(MOD(NF,5).EQ.0) WRITE(6,42,ADVANCE='NO') NF, ' PDFs'//CHAR(13)
      42  FORMAT(I6,A)
    

    gave: 5 PDFs

    leaving the cursor at the #1 position on the same line. On the next update, the 5 turned into a 10. ASCII 13 (decimal) is a carriage return.

    0 讨论(0)
  • 2021-01-05 05:40

    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.

    0 讨论(0)
  • 2021-01-05 05:46

    The following should be portable across systems by use of ACHAR(13) to encode the carriage return.

              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)
    
    0 讨论(0)
  • 2021-01-05 05:55
    OPEN(6,CARRIAGECONTROL ='FORTRAN')
    DO I=1,5
    WRITE(6,'(1H+" ",I)') I
    ENDDO
    
    0 讨论(0)
提交回复
热议问题