首页 > 代码库 > Fortran主程序参数及简单文件操作

Fortran主程序参数及简单文件操作

1. C语言里,主函数main()是可以带参数的,而且如果带参数,只能是两个参数。
  main(int argc, char * argv[]){}

这里,如果在cmd里运行程序,程序文件名本身也算一个参数,因此argc = 输入参数个数+1. 而argv[0]存放的就是程序文件名。

 

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   !default10     ELSEIF(nargin==1) THEN11         CALL getarg(1, FILEIN);         !Set input file only12     ELSE13         CALL getarg(1, FILEIN);         !Set both input and output files14         CALL getarg(2, FILEOUT);15     ENDIF16 17     <Other code>18     19     stop20 END MAIN

 

3. 对于Fortran 2003以后的版本,用如下函数获取参数

函数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 line

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程序定位文件指针到结尾

  将文件指针调整到文件头可以用rewind()函数

  将文件指针调整到文件结尾:可用于判断文件是否完整:在文件结尾设置结尾标记符号,如果遍历一次文件,到结尾时都没有发现”结尾标记“,说明文件不完整。

character bufferdo     read(1,"(A)",iostat=stat1) buffer    if(stat1/=0) exit !when file end ,skip to cycleenddo

 

Fortran主程序参数及简单文件操作