main(int argc, char * argv[]) { }
這裏,若是在cmd裏運行程序,程序文件名自己也算一個參數,所以argc = 輸入參數個數+1. 而argv[0]存放的就是程序文件名。ios
agrc=iargc()
返回命令行參數的個數函數
call getarg(i,charstring)
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
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