Fortran:管道编程 [英] Fortran: pipe to program

查看:160
本文介绍了Fortran:管道编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能从Fortran启动外部程序并为此程序标准输入写入内容?



我知道首先,如果你使用的是一个相对较新的编译器,你应该能够使用这个选项。使用 execute_command_line (f2008规范的一部分)而不是 system (编译器扩展名)。这将使用 C 库的 system 调用启动命令,该调用使用 sh nix上的shell和Windows上的 cmd.exe (请参阅这里)。因此,您可以使用标准输入重定向类型方法来连接已启动程序的 stdin ,但它可能不适合更复杂的使用。



以下示例显示了一个简单示例

 程序stdIn 
隐式无
字符(len = 20):: cmd,args
字符(len = 50):: fullcmd
cmd =bc
args =1 + 2
fullcmd = cmd //<<<// args
print *,Running,fullcmd
call execute_command_line(fullcmd)
end program stdIn

应该输出

 运行bc< 1 + 2 
3


Is there any possibility to launch an external program from Fortran and write something to this programs standard input?

I know e.g. of gfortran's SYSTEM but there is no such option.

解决方案

Firstly, if you're using a relatively recent compiler you should be able to use execute_command_line (part of the f2008 spec) instead of system (compiler extension). This launches a command using the C library's system call which uses the sh shell on nix and cmd.exe on Windows (see here). As such you can use standard input redirection type approaches to connect to stdin of the launched program, but it may not be suitable for more complicated use.

The following example shows a simple example

program stdIn
  implicit none
  character(len=20) :: cmd, args
  character(len=50) :: fullcmd
  cmd = "bc"
  args = "1+2"
  fullcmd = cmd//" <<< "//args
  print*,"Running ",fullcmd
  call execute_command_line(fullcmd)
end program stdIn

Which should output

 Running bc                   <<< 1+2
 3

这篇关于Fortran:管道编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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