本文介绍了打印和写入一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在屏幕上打印某些内容,同时将正在打印的内容也写入文件中?现在,我有这样的事情:

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

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

我觉得我在浪费行数并使代码变得更长.我实际上想打印/写更多的行,所以这就是为什么我正在寻找一种更清洁的方式来做到这一点.

I feel like I'm wasting lines and making the code longer that it should be. I actually want to print/write much more lines that these, so that's why I'm looking for a cleaner way to do it.

推荐答案

写入标准输出和写入文件是两件不同的事情,所以你总是需要单独的指令.但是你不必为你写的每一行打开和关闭文件.

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

老实说,我不认为这是一个更大的努力:

Honestly, I don't think it's that much more of an 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)

这仅比您在每个 write 语句中必须执行的操作多出一行.如果你真的认为你的代码中有太多的 write 语句,这里有一些你可以尝试的想法:

this is only one line more than what you would have to do anyway per write statement.If you really think that you have too many write statements in your code, here are a few ideas that you might try:

如果您在 Linux 或 Unix 系统(包括 MacOS)上运行,您可以编写一个只写入标准输出的程序,并将输出通过管道传输到一个文件中,如下所示:

If you are running on a Linux or Unix system (including MacOS), you can write a program that only writes to standard out, and pipe the output into a file, like this:

$ ./my_program | tee result.txt

这会将数据输出到屏幕,并将其写入文件result.txt

This will both output the data to the screen, and write it into the file result.txt

或者您可以将输出写入程序中的文件,并在外部关注"该文件:

Or you could write the output to a file in the program, and 'follow' the file externally:

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

我刚刚有了另一个想法:如果你真的经常遇到需要将数据同时写入屏幕和文件的问题,你可以将它放入一个子程序中:

I just had another idea: If you really often have the issue that you need to write data to both the screen and the file, you can place that into a subroutine:

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 值,对于其他(integercharacter 等)或您的组合'd 仍然需要有两个 write 语句,或者编写其他特定的write to both"例程.

I am passing the values to be written here as an array because that gives you more flexibility in the number of variables that you might want to print. On the other hand, you can only use this subroutine to write real values, for others (integer, character, etc) or combinations thereof you'd need to still have two write statements, or write other specific 'write to both' routines.

这篇关于打印和写入一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 13:10