执行命令行并返回命令输出
目前,我正在使用非标准 SYSTEM 内在例程(类似于 Fortran 2008 EXECUTE_COMMAND_LINE 内在例程)从我的 fortran 程序中使用 shell 命令行调用:
CALL SYSTEM(commandStr)
其中 commandStr 是包含我要执行的 shell 命令的字符串。目前,我不知道返回 commandStr 输出的直接方法,而只知道它的返回状态。因此,我现在要做的是将输出写入文件,然后从 Fortran 程序中读取该文件。示例:
CALL SYSTEM('sed ''s/,//g'' myFile > dummyFile')
如果我想从 myFile 中删除逗号。然后我使用 OPEN 和 READ 来获取 dummyFile 的内容。
这工作得很好,但是我担心从磁盘写入/读取文件,特别是如果我在一个长循环中执行此操作,并且 commandStr 输出很大。有没有办法将 commandStr 输出重定向到内存缓冲区(而不是硬盘),我可以直接从我的 Fortran 程序访问它(也许通过特定的 UNIT 编号)?
Currently, I am using shell command line calls from my fortran program using non-standard SYSTEM intrinsic routine (similar to Fortran 2008 EXECUTE_COMMAND_LINE intrinsic):
CALL SYSTEM(commandStr)
where commandStr is a character string containing the shell command I want to execute. At the moment, I am not aware of a direct way to return the output of commandStr, but only its return status. So, what I am doing now is writing output into a file, and then reading the file from within the Fortran program. Example:
CALL SYSTEM('sed ''s/,//g'' myFile > dummyFile')
if I want to remove commas from myFile. I then use OPEN and READ to get contents of dummyFile.
This works just fine, however I am concerned about writing/reading files from disk, especially if I was doing this within a long loop, and if commandStr output was big. Is there a way to re-direct commandStr output into a memory buffer (not hard disk) which I could access from my Fortran program directly (maybe through a specific UNIT number)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果这是在 POSIX 环境中,则库函数
popen()
也可能可用。请查看 Fortran 环境的文档,因为我不确定将 i/o 连接到 Fortran 的语义。如果像C运行时库一样,文件连接也需要一个特殊的函数来关闭它,
pclose()
。If this is in a POSIX environment, the library function
popen()
might also be available.Look at the documentation for your Fortran environment since I'm not sure of the semantics for connecting the i/o to Fortran. If it is like the C runtime library, the file connection also needs a special function to close it,
pclose()
.