执行命令行并返回命令输出

发布于 2024-12-11 05:02:04 字数 570 浏览 0 评论 0原文

目前,我正在使用非标准 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静若繁花 2024-12-18 05:02:04

如果这是在 POSIX 环境中,则库函数 popen() 也可能可用。

iunit = popen ('sed ''s/,//g'' myFile', 'r')

请查看 Fortran 环境的文档,因为我不确定将 i/o 连接到 Fortran 的语义。如果像C运行时库一样,文件连接也需要一个特殊的函数来关闭它,pclose()

If this is in a POSIX environment, the library function popen() might also be available.

iunit = popen ('sed ''s/,//g'' myFile', 'r')

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().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文