如何将shell命令添加到Fortran脚本中 [英] How to add shell command into a Fortran script
问题描述
是否可以从Fortran脚本中调用shell命令?
我的问题是我分析了真正大的文件。这些文件有很多行,例如84084002或类似的。
在开始分析之前,我需要知道文件有多少行,因此我通常使用shell命令: wc -lfilename
,并且在我的脚本中使用这个数字作为一个变量的参数。
但是我想从我的程序中调用这个命令并使用行数并将它存储到变量值。
我知道这可能无法正常工作,但如果确实如此,请告诉我。
execute_command_line
,它大致确实执行了广泛实现但非标准的子程序 system
。由于@MarkSetchell已经(几乎)写入了,你可以试试 CALL execute_command_line('wc -l< file.txt> wc.txt')
OPEN(unit = nn,file ='wc.txt')
READ(nn,*)count
Fortran不具备的一种标准方法是在无需使用上述与操作系统相关的解决方法的情况下获取文件中的行数。其他的,也就是打开文件,计算行数,然后倒退到文件的开始处开始阅读。
Is it possible to call shell command from a Fortran script?
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.
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
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屋!