Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29258 Discussions

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

Johannes_Rieke
New Contributor III
4,222 Views
Hi there,

I took the title from a thread in an other forum because it is very interesting and not all solutions works with ifort in windows.

Background: If you like to give users feedback in a console application on the progress of some loops and don't want to spam the output with new lines, a way is needed to stay in one line.

This code I found on the web (http://stackoverflow.com/questions/1390125/how-do-i-format-a-print-or-write-statement-to-overwrite-the-current-line-on-the):
[fortran]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[/fortran] It has the problem that the text is appended in sequence in one line, but previous written characters are not overwritten. At my system (Win7, VS2010, ifort 12.1.5.344) the code don't work. The line with the output stays blank. If I write a new line after the end of the loop the blank line is printed on screen also. What is wrong there? Writing achar(13) as first char had not worked also.

In the same thread one solution is found that works (my version of it):

[fortran]open(6,carriagecontrol ='fortran') write(*,*) 'SCREEN TEST' write(*,*) '"""""""""""' write(*,*) k = 1 do i=1,100 if (k .gt. 4) k = 1 r0percent = real(i)/real(100) * 100.0 if (k .eq. 1) then write(6,'("+ |",1f10.2,"%")') r0percent else if (k .eq. 2) then write(6,'("+ /",1f10.2,"%")') r0percent else if (k .eq. 3) then write(6,'("+ -",1f10.2,"%")') r0percent else if (k .eq. 4) then write(6,'("+ \",1f10.2,"%")') r0percent end if call sleepqq(50) k = k + 1 enddo [/fortran] I wonder if there is a modern way to code this?

Best regards,
Johannes
0 Kudos
8 Replies
joerg_kuthe
Novice
4,222 Views

Two alternatives I can offer (but I wouldn't call them "modern"):

[bash] CHARACTER :: CR = CHAR(13) ! carriage return character

DO Number = 1, MAXIMUM !0 WRITE(*, 100, ADVANCE='NO') REAL(Number)/REAL(Maximum)*100, CR ! cannot use ADVANCE='NO' since output seems to be buffered and only the last WRITE is displayed WRITE(*, 100) REAL(Number)/REAL(Maximum)*100, CR 100 FORMAT(TL10, F10.2, A) ! backslash edit descriptor (not a Fortran standard) suppresses linefeed ! Calcultations on Number call sleepqq(100) END DO
[/bash]
The other one is a commercial Fortran library my company offers:
qtConsole
Here is an example of the output of the demo program:
Example output using qtConsole
qtConsole has a function which allows to position the cursor in the console
window (DOS window) and start writing at that position. Here is a link if you
want to find out more:
http://www.qtsoftware.de/oxShop/en/Libraries-Tools/for-GUI-Design/qtConsole.html

Kind regards,

Joerg Kuthe
www.qtsoftware.com
0 Kudos
Ilie__Daniel
Beginner
4,222 Views
The simplest way is to move the cursor where you want to start writting, then write the line
You can use the Windows APIs to do this.

  1. You need to declare a variable of type(T_COORD) and fill in the co-ordinates; let us call this position
  2. Get the Console window output handle: handle=GetStdHandle(STD_OUTPUT_HANDLE)
  3. Move the cursor using SetConsoleCursorPosition(handle,position)
Check the MSDN for the full description of these procedures.
0 Kudos
Steven_L_Intel1
Employee
4,222 Views
There is no Fortran standard way to do what you want. Using ADVANCE='NO' works on some implementations, but not ours. The variant using CARRIAGECONTROL='FORTRAN' and the + character does work in Intel Fortran, but the notion of Fortran carriage control has been deleted from the Fortran standard. There are various other ways to do this, none of them simple.
0 Kudos
Johannes_Rieke
New Contributor III
4,222 Views
Thanks Joerg, thanks David and thanks Steve!

I conclude that "no advance" and carriage return in console windows are depending very much on the used compiler and system and should be used with care.

However, I will branch my project into one stand alone application in Windows and one Linux server application in a MDO process chain. For the latter one I don't need the progress information on console because everything is dumped into an log file. The Windows version will get sooner or later a gui, so there will be other options for progress information. The solution for the current console application is tentative but I will stay there until the gui version. Maybe I will have a closer look on the Windows APIs, but I am currently not familiar with it...


Best regards,
Johannes
0 Kudos
reidar
New User
4,222 Views
Hello Johannes

Below is a small program that repeatedly overprint a line on the console..
To make it workyou have to set the project property -> Fortran -> Compability -> Use Other Power Station Run-time .. = Yes

Best regards
Reidar



program

integer

write

write

write

do

write

enddo

911

FORMAT('+'T2,'CASE ',I3,' OF ',I3,' COMPUTED')

pause

end


(*,911),i,istop
i =1,100
(*,*)'............'
(*,911),i,istop
(*,*)' '
I,istop

istop=100

Consol
0 Kudos
reidar
New User
4,222 Views
Once more.....

Below is a small program that repeatedly overprint a line on the console..
To make it workyou have to set the project property -> Fortran -> Compability -> Use Other Power Station Run-time .. = Yes

Best regards
Reidar



program

Consol

integer

istop=100

I,istop

write

(*,*)' '

write

(*,911),i,istop

write

(*,*)'............'

do

i =1,100

write

(*,911),i,istop

enddo

911

FORMAT('+'T2,'CASE ',I3,' OF ',I3,' COMPUTED')

pause

end

0 Kudos
nvaneck
New Contributor I
4,222 Views

Simplest I found is:

WRITE(*,"(' Reading data... ',I0,A,$)") K,CHAR(13)

But of course it is not standard fortran to use $. Everything else seems very cumbersome.

0 Kudos
reidar
New User
4,222 Views
I try once more.....

Below is a small program that repeatedly overprint a line on the console..
The clue is the "+" sign at the beginning of the format specification, from the time of
Microsoft Fortran Powerstation...

To make it workyou have to set the project property -> Fortran -> Compability -> Use Other Power Station Run-time .. = Yes

Best regards
Reidar

911 FORMAT('+'T2,'CASE ',I3,' OF ',I3,' COMPUTED')



program Consol

integer i,ISTOP

istop=100
write (*,*)' '

write (*,911),i,istop

write(*,*)'............'

do i =1,100
write(*,911),i,istop
enddo

911 FORMAT('+'T2,'CASE ',I3,' OF ',I3,' COMPUTED')

pause
end

0 Kudos
Reply