如何添加 shell 命令并在 Fortran 程序中使用结果? [英] How to add a shell command and use the result in a Fortran program?

查看:28
本文介绍了如何添加 shell 命令并在 Fortran 程序中使用结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以从 Fortran 脚本调用 shell 命令?

Is it possible to call shell command from a Fortran script?

我的问题是我分析非常大的文件.这些文件有很多行,例如84084002 或类似.在开始分析之前,我需要知道文件有多少行,因此我通常使用 shell 命令:wc -l "filename",然后将此数字用作一个变量的参数在我的脚本中.

My problem is that I analyze really big files. These files have a lot of lines, e.g. 84084002 or similar. I need to know how many lines the file has, before I start the analysis, therefore I usually used shell command: wc -l "filename", and than used this number as a parameter of one variable in my script.

但我想从我的程序中调用这个命令并使用行数并将其存储到变量值中.

But I would like to call this command from my program and use the number of lines and store it into the variable value.

推荐答案

自 1984 年以来,实际上在 2008 标准中但已经被大多数常见的 Fortran 编译器实现,包括 gfortran,有一个标准内在子例程 execute_command_line 大致完成了广泛实施但非标准子例程 system 所做的事情.正如@MarkSetchell(几乎)所写,您可以尝试

Since 1984, actually in the 2008 standard but already implemented by most of the commonly-encountered Fortran compilers including gfortran, there is a standard intrinsic subroutine execute_command_line which does, approximately, what the widely-implemented but non-standard subroutine system does. As @MarkSetchell has (almost) written, you could try

CALL execute_command_line('wc -l < file.txt > wc.txt' ) 
OPEN(unit=nn,file='wc.txt') 
READ(nn,*) count 

Fortran 所没有的是一种标准方法,可以在不求助于上述依赖于操作系统的解决方法的情况下获取文件中的行数.其他,即打开文件,计算行数,然后倒退到文件开头开始阅读.

What Fortran doesn't have is a standard way in which to get the number of lines in a file without recourse to the kind of operating-system-dependent workaround above. Other, that is, than opening the file, counting the number of lines, and then rewinding to the start of the file to commence reading.

这篇关于如何添加 shell 命令并在 Fortran 程序中使用结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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