Fortran主程序參數及簡單文件操做

1. C語言裏,主函數main()是能夠帶參數的,並且若是帶參數,只能是兩個參數。
  main(int argc, char * argv[])
{
}

這裏,若是在cmd裏運行程序,程序文件名自己也算一個參數,所以argc = 輸入參數個數+1. 而argv[0]存放的就是程序文件名。ios

 

2. 在Fortran中主函數是沒有參數的,若是要再命令行中執行Fortran程序,向Fortran中傳遞參數,須要在程序中調用相應的函數。
  
agrc=iargc()

  返回命令行參數的個數函數

  
call getarg(i,charstring)
  讀取命令行的第i個參數,並將其存儲到charstring中,其中命令自己是第0個參數
 1 Example: 
 2     ! procedure has 2 arguments, one is input file, other is output file
 3 PROGRAM MAIN
 4     IMPLICIT NONE
 5     INTEGER argc
 6     character*60 FILEIN,FILEOUT
 7     
 8     IF(nargin==0) THEN
 9         FILEIN      ='FILE.IN'   !default
10     ELSEIF(nargin==1) THEN
11         CALL getarg(1, FILEIN);         !Set input file only
12     ELSE
13         CALL getarg(1, FILEIN);         !Set both input and output files
14         CALL getarg(2, FILEOUT);
15     ENDIF
16 
17     <Other code>
18     
19     stop
20 END MAIN

 

3. 對於Fortran 2003之後的版本,用以下函數獲取參數spa

函數1:COMMAND_ARGUMENT_COUNT() — Get number of command line arguments命令行

  這是一個function,有返回值。指針

Example: 
          program test_command_argument_count 
              integer :: count 
              count = command_argument_count() 
              print *, count 
          end program test_command_argument_count 

 

子程序2:GET_COMMAND_ARGUMENT  相似於getarg()子程序
用法:CALL GET_COMMAND_ARGUMENT(NUMBER [, VALUE, LENGTH, STATUS]) 
詳解:
  NUMBER是獲取第幾個參數,VALUE是相應的值;
      (讓NUMBER=0獲得的是可執行程序的名字,若是輸入參數個數比NUMBER小,獲得的爲空。)
  LENGTH是第NUMBER個參數的長度;
        STATUS是獲取這個參數後的狀態
      (若是取這個參數錯誤,返回的是正數;若是VALUE是一個 truncated的參數,那麼返回-1;其它狀況返回0)
Return value:
The return value is an INTEGER of default kind.
Example:
          program test_command_argument_count
              integer :: count
              count = command_argument_count()
              print *, count
          end program test_command_argument_count

 

 

子程序3:GET_COMMAND — Get the entire command linecode

Description:
Retrieve the entire command line that was used to invoke the program.
Standard:
Fortran 2003 and later
Class:
Subroutine
Syntax:
CALL GET_COMMAND([COMMAND, LENGTH, STATUS])
Arguments:
COMMAND (Optional) shall be of type CHARACTER and of default kind.
LENGTH (Optional) Shall be of type INTEGER and of default kind.
STATUS (Optional) Shall be of type INTEGER and of default kind.
Return value:
If COMMAND is present, stores the entire command line that was used to invoke the program in COMMAND. If LENGTH is present, it is assigned the length of the command line. If STATUS is present, it is assigned 0 upon success of the command, -1 if COMMAND is too short to store the command line, or a positive value in case of an error.
Example:
          PROGRAM test_get_command
            CHARACTER(len=255) :: cmd
            CALL get_command(cmd)
            WRITE (*,*) TRIM(cmd)
          END PROGRAM

 

4 .Fortran程序定位文件指針到結尾blog

  將文件指針調整到文件頭能夠用rewind()函數ip

  將文件指針調整到文件結尾:可用於判斷文件是否完整:在文件結尾設置結尾標記符號,若是遍歷一次文件,到結尾時都沒有發現」結尾標記「,說明文件不完整。get

character buffer
do 
    read(1,"(A)",iostat=stat1) buffer
    if(stat1/=0) exit !when file end ,skip to cycle
enddo
相關文章
相關標籤/搜索