有没有办法将命令行实用程序的输出存储到Fortran中的变量?
我有一个基于BASH的实用程序,它给了我一个需要在Fortran程序中使用的数字.我想通过程序本身调用该实用程序,并尽可能避免将输出写入文件.
这样的事可能吗?
integer a
write(a,*) call execute_command_line('echo 5')
Run Code Online (Sandbox Code Playgroud)
或者像这样呢?
read(call execute_command_line('echo 5'),*) a
Run Code Online (Sandbox Code Playgroud)
我不认为这些都是正确的.我想知道是否有一种方法可以做到这一点.我阅读了文档,execute_command_line但我不认为子例程有一个输出参数.
由于您使用的是BASH,我们假设您正在使用某种类似unix的系统.所以你可以使用FIFO.就像是
program readfifo
implicit none
integer :: u, i
logical :: ex
inquire(exist=ex, file='foo')
if (.not. ex) then
call execute_command_line ("mkfifo foo")
end if
call execute_command_line ("echo 5 > foo&")
open(newunit=u, file='foo', action='read')
read(u, *) i
write(*, *) 'Managed to read the value ', i
end program readfifoRun Code Online (Sandbox Code Playgroud)
注意FIFO的wrt阻塞的语义可能有点棘手(这就是为什么在echo命令之后有'&'的原因,你可能想要读一下它并进行实验(特别是确保你没有得到多次执行此操作时,bash进程会挂起).
| 归档时间: |
|
| 查看次数: |
274 次 |
| 最近记录: |