Print and write on one line?

Is it possible to print something on the screen and, at the same time, what is printed is also written to the file? Right now I have something like this:

print *, root1, root2
open(unit=10,file='result.txt'
write(10,*), root1, root2
close(10)

It seems to me that I'm wasting lines and making code longer than it should be. I really want to print / write a lot more lines to get this, so I'm looking for a cleaner way to do this.

+2
source share
1 answer

Writing to standard output and writing to a file are two different things, so you will always need separate instructions. But you do not need to open and close the file for every line you write.

Honestly, I don’t think it is much more effort:

open(unit=10, file='result.txt', status='replace', form='formatted')
....
write( *, *) "Here comes the data"
write(10, *) "Here comes the data"
....
write( *, *) root1, root2
write(10, *) root1, root2
....
close(10)

, . , , , :

Linux Unix ( MacOS), , , , :

$ ./my_program | tee result.txt

result.txt

"" :

$ ./my_program &
$ tail -f result.txt

: , , :

program my_program
    implicit none
    real :: root1, root2, root3
    ....
    open(10, 'result.txt', status='replace', form='formatted')
    ....
    call write_output((/ root1, root2 /))
    ....
    call write_output((/ root1, root2, root3 /))
    ....
    call write_output((/ root1, root2 /))
    ....
    close(10)
    ....
contains
    subroutine write_output(a)
        real, dimension(:), intent(in) :: a
        write( *, *) a
        write(10, *) a
    end subroutine write_output
end program my_program

, , , . , real, (integer, character ..) write " " .

+7

Source: https://habr.com/ru/post/1615151/


All Articles