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