有一种传统的方法可以逐个读取每一行,并在每次阅读时检查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
这样的变量吗?也就是说,我想在最后一个打印命令中打印变量而不是'< ? >'
。
答案 0 :(得分:1)
这是不可能的直接方式。您可以将命令的输出重定向到文件,然后将其打开并读取它http://compgroups.net/comp.lang.fortran/how-to-get-the-output-of-call-system-in-a-v/216294
或者使用Unix函数的一些更复杂的功能并调用它的C API(参见该线程中的第一个答案)。
EXECUTE_COMMAND_LINE()也没有任何功能可以直接读取命令的输出。
答案 1 :(得分:1)
如果要使用Unix命令$ wc -l
,可以调用Fortran子例程execute_command_line
,这是许多Fortran编译器所共有的,gfortran
包含在内。
这是一个工作示例,它计算名为nlines
的文件的行数style.gnuplot
,然后使用nlines
通过覆盖将某些行附加到style.gnuplot
最后一个。
PROGRAM numLines
IMPLICIT NONE
integer, parameter :: n = 100
integer :: i, nLines
real, parameter :: x0 = -3.14, xEnd = 3.14
real :: dx
real, dimension (:), allocatable :: x, fun
allocate(x(0:n)) ! Allocate the x array
allocate(fun(0:n)) ! Allocate the fun array
dx = abs(xEnd-x0)/n
x(0:n) = [(x0+i*dx, i = 0,n)] ! Create the x array
fun(0:n) = [(sin(x0+i*dx), i = 0,n)] ! Create the fun array
open(unit=1,file="plotFunction.dat")
DO i=0,size(x)-1
write(1,*) x(i), ' ', fun(i) ! Save the function to a file to plot
END DO
close(unit=1)
deallocate(x) ! Deallocate the x array
deallocate(fun) ! Deallocate the fun array
open(unit=7, file="style.gnuplot")
write(7,*) "set title 'y = sin(x)' font 'times, 24'"
write(7,*) "set tics font 'times, 20'"
write(7,*) "set key font 'times,20'"
write(7,*) "set grid"
write(7,*) "set key spacing 1.5"
write(7,*) "plot '<cat' u 1:2 w l lw 2 linecolor rgb 'orange' notitle "
close(unit=7)
CALL execute_command_line("wc -l style.gnuplot | cut -f1 -d' ' > nlines.file") ! COunt the lines
open(unit=1,file='nlines.file')
read(1,*) nlines ! Here the number of lines is saved to a variable
close(unit=1)
CALL execute_command_line("rm nlines.file") ! Remove nlines.file
CALL execute_command_line("cat plotFunction.dat | gnuplot -p style.gnuplot") ! Show the plot within the executable
open(unit=7,file="style.gnuplot")
DO i = 1,nLines-1
read(7,*) ! Read the file untile the penultimate row,
END DO ! then append the other rows
write(7,*) "set object rectangle at -3.14,0 size char 1, char 1", &
" fillcolor rgb 'blue' fillstyle solid border lt 2 lw 1.5"
write(7,*) "set object rectangle at 0,0 size char 1, char 1", &
" fillcolor rgb 'blue' fillstyle solid border lt 2 lw 1.5"
write(7,*) "set object rectangle at 3.14,0 size char 1, char 1", &
" fillcolor rgb 'blue' fillstyle solid border lt 2 lw 1.5"
write(7,*) "plot 'plotFunction.dat' u 1:2 w l lw 2 linecolor rgb 'orange' notitle"
close(unit=7)
CALL execute_command_line("gnuplot -p 'style.gnuplot'") ! Load again style.gnulot with the appended lines
END PROGRAM numLines
我的代码可能不太优雅,但似乎有效!