打印并写入一行? [英] Print and write in one line?

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

问题描述

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

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

我感觉自己在浪费线条,并且让代码变得更长,我实际上想要打印/写更多的线条,所以这就是为什么我要找一个更干净的方式来做到这一点。
解决方案

标准输出写入文件是两个不同的东西,所以你将永远需要单独的指令,但是你不必打开和关闭每一行你写的文件。

老实说,我不认为这是更多的努力:

  open(unit = 10,file ='result.txt',status ='replace',form ='formatted' )
...
write(*,*)这里是数据
write(10,*)这里是数据
....
写(*,*)root1,root2
写(10,*)root1,ro ot2
....
close(10)

这只是一个每一个写声明都要比你要做的更多。
如果你真的认为你的代码中有太多的写入语句,这里有一些你可能会尝试的想法:

如果你正在Linux或Unix系统(包括MacOS),你可以编写一个只写入标准输出的程序,并将输出传送到一个文件中,如下所示:

  $ ./my_program | tee result.txt 

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



您也可以将输出写入程序中的文件, :

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

我还有另一个想法:有这个问题,你需要写数据到屏幕和文件,你可以把它放到一个子程序中:

 程序my_program 
implicit none
real :: root1,root2,root3
....
open(10,'result.txt',status ='replace',form =' ((/ root1,root2 /))
....
调用write_output((/ root1,root2,root3 /)
调用write_output )
....
call write_output((/ root1,root2 /))
....
close(10)
....
包含
子程序write_output(a)
实数,维(:),意图(in):: a
写(*,*)a
写(10,* )a
结束子程序write_output
结束程序my_program

写在这里作为一个数组,因为这给你更多的灵活性你可能想要打印的变量的数量。另一方面,你只能用这个子程序写 real 值,对于其他的( integer character 等)或者它们的组合,你仍然需要两个 write 语句,或者写其他特定的'write to both'例程。

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)

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:

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

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

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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆