- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
Link Copied
8 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Two alternatives I can offer (but I wouldn't call them "modern"):
[bash] CHARACTER :: CR = CHAR(13) ! carriage return characterDO 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:

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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
You can use the Windows APIs to do this.
- You need to declare a variable of type(T_COORD) and fill in the co-ordinates; let us call this position
- Get the Console window output handle: handle=GetStdHandle(STD_OUTPUT_HANDLE)
- Move the cursor using SetConsoleCursorPosition(handle,position)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
(*,911),i,istop i =1,100(*,*)'............'(*,911),i,istop(*,*)' ' I,istop
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- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
Consolinteger
istop=100
I,istopwrite
(*,*)' 'write
(*,911),i,istopwrite
(*,*)'............'do
i =1,100write
(*,911),i,istopenddo
911
FORMAT('+'T2,'CASE ',I3,' OF ',I3,' COMPUTED')pause
end
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page