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

refresh standard output

blissmj
Beginner
1,281 Views

I am trying to show progress of the program on the screen (standard output). I would like to refresh the current line it is writing to. For example, I want it to simply print the current index to the screen, but I don't want it to print the index each time on a new, seperate line.

I think the rewrite command would work for a regular file, but get an error on the following line of code:

rewrite(*,*)'Progress: ',i,' of 100'

What I want:

Progress:3 of 100

What I don't want:

Progress 1 of 100

Progress: 2 of 100

Progress: 3 of 100 etc...

Thanks!

0 Kudos
8 Replies
Les_Neilson
Valued Contributor II
1,281 Views

Try something like this

write (*,'(a,i0,a,$)') 'Progress: ',i,' of 100'

Les

0 Kudos
TimP
Honored Contributor III
1,281 Views
Did you want
write (*,"(t1,a,i0,a,advance='no')") 'Progress: ',i,' of 100'
0 Kudos
Les_Neilson
Valued Contributor II
1,281 Views

Why is it I can neverremember the "Advance=NO" option ?
To do my reply I justhad a quick look at some codeweuse for doing exactly this sort of "progress" thing andmerely copied thewrite statementas is. One day I shall ask permission to change the code.

Les

0 Kudos
blissmj
Beginner
1,281 Views

Thanks for the ideas, however, it's still not working...

write

(*,'(t1,a,i0,a,advance=no)') 'Progress: ',i,' of 100'

didn't work - compiler complained that 'A constant or general expression must appear in a format list in this context. [,a,advance=no]

It also complained that characters =, c, n and v are not valid in a format list.

I tried pulling the advance='no' out of the format list, but then it doesn't write anything to the screen

write(*,'(t1,a,i0,a)',advance='no') 'Progress: ',i,' of 100'

I also tried Les's line of code write (*,'(a,i0,a,$)') 'Progress: ',i,' of 100'

But only get a continuous stream of output. I'll admit, I have no idea what the $ does.

Progress: 1 of 100Progress: 2 of 100Progress:3 of 100Progress: 4 of 100Progress:5 of 100 etc....

Any other suggestions?

0 Kudos
Steven_L_Intel1
Employee
1,281 Views
This works:

use ifcore

write (*,*)

do i=1,10
write (*,'("Testing ",I2,A,$)') i,char(13)
call sleepqq(1000)
end do
write (*,'(A)') char(10)
end

The $ suppresses the newline at the end of the line. ADVANCE='NO' (which is a separate WRITE keyword and not part of the format) does not work for this as it does not move the cursor back to the beginning of the line. I found it also necessary to write CR and LF characters to get things positioned right. The call to sleepqq was just for the purpose of the test.

0 Kudos
Les_Neilson
Valued Contributor II
1,281 Views

Sorry my mistake. Looking closer atour actual code that does a progress count in fact it writes a number ofbackspaces ( using bs = char(8) ) before writing the string.
The code I showed before isusedtodisplay a prompt andget a user response on the same line.

The $ is an old extension before advance='no' came about.

Les

0 Kudos
blissmj
Beginner
1,281 Views

Thank you!

The only limitation is the line of text cannot be longer than 1 line. I added text that inadvertantly split across two lines of the standard output and it doesn't work in that case. If the length is less than the standard output record length it works.

I'm not clear how the char(13) and char(10) actually make this work, but thanks

Matt

0 Kudos
Steven_L_Intel1
Employee
1,281 Views
There's no way to make it work across multiple lines unless you get into calling Win32 API routines to reposition the cursor in the terminal window.

CHAR(13) is CR - it moves the cursor to the beginning of the current line. CHAR(10) is LF - it moves the cursor to the next line.
0 Kudos
Reply