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
相关文章
相关标签/搜索