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

查看:57
本文介绍了如何添加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.

我知道那可能行不通,但是如果可以,请告诉我.

I know that probably, that can't work, but if it does, please let me know.

推荐答案

自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天全站免登陆