Save command line output to variable in Fortran

我怕爱的太早我们不能终老 提交于 2019-12-04 14:05:42

Since you're using BASH, lets assume you're working on some kind of unix-like system. So you could use a FIFO. Something like

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 readfifo

Note that the semantics of FIFO's wrt blocking can be a bit tricky (that's why there is the '&' after the echo command, you might want to read up on it a bit and experiment (particularly make sure you haven't got a zillion bash processes hanging around when you do this multiple times).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!