zhenghui2915的个人博客分享 http://blog.sciencenet.cn/u/zhenghui2915

博文

【Fortran】Fortran程序带参数运行方法总结

已有 17629 次阅读 2012-10-14 17:37 |个人分类:程序设计|系统分类:科研笔记|关键词:学者| 程序

我们知道,通常情况下基于控制台的Fortra源代码在编写完毕后,编译连接后会在当前的工程目录下的Debug目录下生成用工程名命名的可执行文件(*.exe)。这时在命令提示符下进入到该Debug目录下,运行该exe就能执行Fortra程序。但是,有时候我们需要在命令提示符下输入可执行文件名后面添加若干个参数,希望把这些参数传递到Fortran程序中去执行。此时就需要对原来的Fortran源程序进行相应的修改,具体的方法有如下两种。
 
方法一:采用get_command_argument函数
       用法:CALL GET_COMMAND_ARGUMENT(NUMBER [, VALUE, LENGTH, STATUS])其中NUMBER是获取第几个参数,VALUE是相应的值(让NUMBER=0得到的是可执行程序的名字,如果输入参数个数比NUMBER小,得到的为空。);LENGTH是第NUMBER个参数的长度;STATUS是获取这个参数后的状态(如果取这个参数发生错误,返回的是正数;如果VALUE是一个截断的参数,那么返回-1;其它情况返回0)。
 
      举例:
    program Console_Commands
    implicit none
    ! Variables
    integer*4 i,n
    character(len=32) arg 
    integer*4 status
    character(len=32) buffer 
   print *, 'Program Begin'
!   method 1:
    i=1
    do
    call get_command_argument(i,arg)
        if (len_trim(arg)==0) then
            exit
        end if 
          read(arg,'(I3)')n
        write(*,*)"input number n=",n
        write(*,*)n*n
        i=i+1
       end do
  print *, 'Program End'
 


方法二:采用getarg函数
      用法:CALL GETARG (n, buffer [, status])
      参数:
n
(Input) INTEGER(2). Position of the command-line argument to retrieve. The command itself is argument number 0.

buffer
(Output) Character*(*). Command-line argument retrieved.

status
(Optional; output) INTEGER(2). If specified, returns the completion status. If there were no errors, status returns the number of characters in the retrieved command-line argument before truncation or blank-padding. (That is, status is the original number of characters in the command-line argument.) Errors return a value of -1. Errors include specifying an argument position less than 0 or greater than the value returned by NARGS.

Example:
如果命令行是"ANOVA -g -c -a",buffer至少5个字符长度,则:
CALL GETARG (0, buffer, status)
结果:buffer是“ANOVA”,status是5
CALL GETARG (2, buffer, status)
结果:buffer是“-c”,status是2

 
举例:(实现上例的同样功能)
 program Console_Commands
    implicit none
    ! Variables
    integer*4 i,n
    character(len=32) arg 
    integer*4 status
    character(len=32) buffer
 
   print *, 'Program Begin'
!    method 2:
     n=iargc()
!    do i=0,n !程序名本身用参数0表示
    do i=1,n !程序名本身用参数0表示
        call getarg(i,arg)
        read(arg,'(i2)')n
        write(*,*)"input number n=",n
        write(*,*)n*n
    end do
  print *, 'Program End'
 
 
拓展:带各种类型参数的程序
用过DOS或者Linux操作系统的朋友经常会碰到在执行某个程序的时候在该程序名后添加参数的情况,下面的这个例子给出了Fortran程序在程序名后添加字符,字符串,整形,浮点数(实型)数据作为输入参数的情形,今后在编写带参数的Fortran程序时可以参考。
 
实例:
本实例是想通过如下的命令执行程序:
!    console_commands.exe -i inputfile -o outputfile -c + -n 123 -n 456 -r 100.123 -r 200.456
此命令中包含了14个参数(程序名的默认编号为0),依次说明如下:
第1个参数: -i
第2个参数:inputfile
第3个参数: -o
第4个参数:outputfile
第5个参数: -c
第6个参数:+
第7个参数: -n
第8个参数:123
第9个参数: -n
第10个参数:456
第11个参数: -r
第12个参数:100.123
第13个参数: -r
第14个参数:200.456
程序运行时,首先依次读取并输出这14个参数,然后利用第6的参数的操作对第12和第14这两个参数进行四则运算。
 
完整的源代码如下:
!  Console_Commands.f90
!
!  FUNCTIONS:
!  Console_Commands - Entry point of console application.
!
!****************************************************************************
!
!  PROGRAM: Console_Commands
!
!  PURPOSE:  Entry point for the console application.
!
!****************************************************************************
    program Console_Commands
    implicit none
    ! Variables
    integer*4 i,mm,nn          !整数型变量定义
    real*8 firNum,secNum,res   !实型变量定义
    character(len=32) arg      !用来保存输入的参数,应注意其长度应比输入的参数的字符串长度大。
    character(len=1) operater  !用来记录是进行何种四则运算。
    character(len=2) para      !用来记录参数的种类 例如“-i,-o”等
    character(len=32) progName,inputfile,outputfile !字符串型变量定义
    print *, 'Program Begin'
   
!    console_commands.exe -i inputfile -o outputfile -c + -n 123 -n 456 -r 100.123 -r 200.456
    !测试开始
    i=iargc() !参数个数
    write(*,*)'Number of parameter:',i
    call getarg(0,arg)  !输出当前的应用程序名
    read(arg,'(A32)')progName   !注意此处read可以从字符串读取数据到其他类型的变量,
                                !此处将arg变量赋值到proName字符串变量
    write(*,*)progName
   
    call getarg(1,arg)  !-i
    read(arg,'(A2)')para
    print *, para
    call getarg(2,arg)  ! inputfile
    read(arg,'(A32)')inputfile
    print *, inputfile
   
    call getarg(3,arg) ! -i
    read(arg,'(A2)')para
    print *,para
    call getarg(4,arg) ! outputfile
    read(arg,'(A32)')outputfile
    print *,outputfile
   
    call getarg(5,arg) ! -c
    read(arg,'(2A)')para
    print *, para     
   
    call getarg(6,arg) ! +
    read(arg,'(A)')operater
    print *, operater
       
    call getarg(7,arg) ! -mm
    read(arg,'(2A)')para
    print *, para
    call getarg(8,arg)
    read(arg,'(I3)')mm
    print *, mm
   
    call getarg(9,arg) ! -nn
    read(arg,'(2A)')para
    print *, para
    call getarg(10,arg)
    read(arg,'(I3)')nn
    print *, nn
  
    call getarg(11,arg) ! -r
    read(arg,'(2A)')para
    print *, para    
    call getarg(12,arg)
    read(arg,'(F8.3)')firNum
    print *, firNum
      
    call getarg(13,arg) !-r
    read(arg,'(2A)')
    print *, para   
    call getarg(14,arg)
    read(arg,'(F8.3)')secNum
    print *, secNum
   
    !对读取的数据应用操作符进行相应的运算
    if(operater=='+')then
        res=firNum+SecNum
    elseif(operater=='-')then
        res=firNum-SecNum
    elseif(operater=='*')then
        res=firNum*SecNum
    elseif(operater=='/')then
        if(secNum==0)then
           write(*,*)'除法分母为0,非法操作'
           stop
        endif
        res=firNum/SecNum
   endif
 
    write(*,*)'firNum  ', operater ,' secNum =',res
   
    ! Body of Console_Commands
    print *, 'Program End'
    end program Console_Commands
 
运行结果如下:
 
本实例程序在Intel Fortran VS2005 IDE 下调试通过!
源码下载:Console_Commands.rar
 
另一参考Fortran程序:


https://m.sciencenet.cn/blog-51026-622520.html

上一篇:【GMTSAR】GMTSAR软件处理实例终端输出参考
下一篇:【GMT】ps2raster命令设定GMT绘图图片输出格式

2 陈小斌 李政江

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-5-11 20:15

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部