有一种常规方法,逐行读取并检查iostat
每次读数时都会达到非零或负值。不过,我想打电话system(command)
例行公事和
使用wc -l
命令来计算数量,然后想要分配要放置数据的数组的维度。例如,我以两种方式打印行数:
Program Test_reading_lines
integer:: count,ios, whatever
character(LEN=100):: command
Print*,'Reading number of lines in a standard way'
count=0
open (10, file='DATA_FILE')
Do
read (10,*,iostat=ios) whatever
if (ios/=0) exit
count=count+1
End Do
close(10)
Print*,'Number of lines =', count
Print*,'Reading number of lines using shell command'
command='cat DATA_FILE | wc -l'
call system(command)
Print*,'Number of lines =','< ? >'
End Program Test_reading_lines
不幸的是,在后一种情况下,我可以分配一个变量吗?count
就像标准情况一样?也就是说,我想打印一个变量而不是'< ? >'
在最后一个打印命令中。
这不可能以简单的方式实现。您可以将命令的输出重定向到文件,然后打开它并读取它http://compgroups.net/comp.lang.fortran/how-to-get-the-output-of-call-system-in-a-v/216294 http://compgroups.net/comp.lang.fortran/how-to-get-the-output-of-call-system-in-a-v/216294
或者使用 Unix 函数的一些更复杂的功能并调用其 C API(请参阅该线程中的第一个答案)。
The EXECUTE_COMMAND_LINE() https://gcc.gnu.org/onlinedocs/gfortran/EXECUTE_005fCOMMAND_005fLINE.html也没有任何直接读取命令输出的功能。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)