Reputation: 195
I've been searching for a considerably long time for this. Does anyone know how to clear the screen in a console app in Fortran language? any help will be very much appretiated!
Upvotes: 2
Views: 7234
Reputation: 1082
I found yet another way to clear screen in UNIX like systems by printing the output of clear
command
(from manual of clear command which states you can write the output to a file and then cat
it to clear the screen)
So better way is to clear > temp.txt
and use the characters in files in a print statement
Although both do the same thing, calling SYSTEM("foo") is very very (100+ times) slower than directly printing those characters
For example:
program clear
implicit none
INTEGER::i
do i = 1, 1000
print *, "Test"
call system("clear")
enddo
end program clear
This program takes anywhere between 3.1 to 3.4 seconds.
But instead of calling system, if I directly print the characters like this :
program clear
implicit none
INTEGER::i
do i = 1, 1000
print *, "Test"
print *, "[H[2J[3J"
enddo
end program clear
This produces the exact same result but takes anywhere between 0.008 to 0.012 seconds (8 to 12 ms)
In fact, running the second loop 100,000 is faster than CALL SYSTEM("clear")
1000 times
EDIT: Don't copy-paste from here, it won't work, (characters are replaced on StackOverflow)
Just use clear > file
and copy the contents of the file into a print statement
Screenshot of actual code(from preview):
These characters aren't supported I guess, They disappear in the answers (They work fine in preview)
Upvotes: 0
Reputation: 1082
In Fortran ACHAR(N)
returns the ASCII of N, so my preferred method would be:
WRITE(*,'(2f15.9,A5)',advance='no') float1,float2,ACHAR(13)
ACHAR(13)
returns carriage return character \r
in Python. So after printing, it returns the cursor to the beginning of the line where it can be overwritten.
After you are out of the loop you can use CALL SYSTEM('clear')
to clean the screen.
This is helpful since CALL SYSTEM('clear')
is slower and uses a lot of CPU, you can check this by replacing the above method with
WRITE(*,'(2f15.9)',advance='no') float1,float2;CALL SYSTEM('clear')
and check the difference in time taken in loops.
Upvotes: 1
Reputation: 60008
Contrary to others I would not call SYSTEM()
(standard Fortran 2008 alternative is execute_command_line()
) but I would print right ANSI escape code http://en.wikipedia.org/wiki/ANSI_escape_code:
print *, achar(27)//"[2J"
This will be much faster than calling SYSTEM()
.
This works in typical Linux terminals, but will not work in the MS Windows terminal.
Another more practical reference how to use the escape code is http://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html
Upvotes: 3
Reputation: 331
In Fortran 90/95 your best option is the system command which is a vendor supplied extension (i.e., not part of the F90/95 standard so some obscure Fortran compilers may not have it but all major ones do).
$ cat clear.f90
program
call system('clear')
end
$ gfortran clear.f90 -o clear
$ ./clear
Upvotes: 2
Reputation: 23833
It depends on your specific sytem and compiler. There is no general way. Fortran doesn't know about specific hardware devices like terminal screens and printers. (Neither do most other languages). The details depend entirely on your specific system.
My advice would be to clear the terminal by invoking the relevent script via the command line - but this is not nice. it is generally more portable to write the output to an ordinary text file and then execute appropriate system commands to print that file to screen. This way you can manipulate the file as you wish...
See here for a simalar question from which these some of the above text was salvaged.
Upvotes: 1
Reputation: 78316
Fortran, qua Fortran, knows nothing of such concepts as screens or keyboards or, for that matter, computers. There is, therefore, no language-standard way of clearing a screen from Fortran. You will have to find some platform-dependent approach.
Most Fortran compilers have some way of doing this, for example Intel Fortran provides the SYSTEM function.
Upvotes: 5