2014-09-26 75 views
3

有一種逐行讀取每行的傳統方法,並在每次讀取時檢查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

使用'fortran'標記,在你的問題中沒有任何特定的fortran90。即使它是特定的,也可以使用兩個標籤。正因爲如此,你很可能沒有得到任何關注。 – 2014-09-27 09:17:37

+0

謝謝@VladimirF,下次標記時我會記住這一點。 – hbaromega 2014-09-27 12:43:19

回答

1

如果您想使用Unix命令$ wc -l,您可以調用Fortran子程序execute_command_line,這對許多Fortran編譯器都很常用,包括gfortran

這裏是一個工作示例,其計算一個稱爲style.gnuplot文件的行數,nlines,然後使用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 

我的代碼可能不夠優雅,但它似乎工作!

+0

是的,這應該工作。不幸的是,這是間接的方式,當你必須將輸出保存到外部文件並從Fortran中讀取時。 – 2016-12-23 09:49:13

+0

我明白了。使用此解決方案的主要缺點是什麼?它是否會增加很多CPU時間? – 2016-12-23 17:18:15

+0

好吧,當然,任何磁盤輸入和輸出都需要相當長的時間。這取決於你是否關心你的應用程序。 – 2016-12-23 17:23:58