getarg 和 iargc:获取命令行参数

本文介绍Fortran中使用getarg和iargc子程序获取命令行参数的方法及示例代码。getarg用于读取指定索引的命令行参数,iargc则返回参数总数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.4.15 getarg iargc :获取命令行参数

getarg iargc 访问命令行上的参数(在命令行预处理程序扩展后)。

1.4.15.1 getarg :获取命令行参数

该子例程的调用方式如下所示:

call getarg( k , arg )

k

INTEGER*4

输入

参数索引(0=第一个=命令名称)

arg

character* n

输出

k 个参数

n

INTEGER*4

arg 的大小

大得足以容纳最长的参数

 

 

1.4.15.2 iargc :获取命令行参数的数量

该函数的调用方式如下所示:

m = iargc()

返回值

INTEGER*4

输出

命令行中参数的数量

 

 

示例:使用 iargc getarg 获取参数的数量和每个参数:

 

demo% cat yarg.f


character argv*10
INTEGER*4 i, iargc, n
n = iargc()
do 1 i = 1, n
call getarg( i, argv )
1 write( *, ’( i2, 1x, a )’ ) i, argv
end
demo% f95 yarg.f
demo% a.out *.f
1 first.f
2 yarg.f
如何在codeblocks中调试的时候调用输入窗口,以下为主程序代码program main use file_number use file_pairs implicit none !定义变量 integer :: iargc,n,i,k1,j,innfiles,outnfiles,nfiles,ierr character(len=256) :: key,k_value character(len=256),dimension(12) :: input_files,output_files character(len=256), dimension(😃,allocatable :: par !动态分配命令行参数 !定义常量 integer :: max_files=12 n=iargc()!获取命令行参数数量 if (n.lt.3) then!为什么输入文件为2个的时候也要终止?? print*, "错误,至少需要两个输入文件参数" call help stop end if allocate(par(n)) !文件计数器的初始化 innfiles=0 outnfiles=0 input_files='' output_files=''!这个可以没有 do i=1,n call getarg(i,par(i))!读取command命令行里的输入信息(解析命令行参数) k1 = index(par(i),'=') if(k1<=0) then print*,"警告:无效的输入格式",trim(par(i)) call help stop end if key = par(i)(1:k1-1)!寻找记录classi_old/newort k_value=par(i)(k1+1:)!用户自命名的classi_old/newort call extract_file_number(key, j, ierr)!提取文件编号,读取两位数字 if (ierr==1) then goto 999 end if !动态识别文件类型(old/new),并进行名称的存储 if(index(key,'_oldort')>0) then !输入文件的处理 !存储输入文件名称,并记录输入文件的数量 if(j>=1 .and. j<=max_files) then innfiles=innfiles+1 input_files(j)=trim(k_value) else print*,"警告:无效的文件编码",trim(key) end if else if(index(key,'_newort')>0) then !输出文件的处理 !存储输出文件名称,并记录输出文件的数量 if(j>=1 .and. j<=max_files) then outnfiles=outnfiles+1 output_files(j)=trim(k_value) else print*,"警告:无效的文件编码",trim(key) end if end if end do ! 验证文件配对,查看是否满足一新一旧的匹配 call validate_file_pairs(max_files,innfiles,outnfiles,input_files,output_files,nfiles ) ! 调用分类处理子程序 call ort_pick_compare(nfiles,input_files(1:nfiles),output_files(1:nfiles)) deallocate(par) 999 end
最新发布
07-09
编译的时候以下代码出现错误,试着进行修正:program main implicit none !定义变量 integer :: iargc,n,i,k1,j,innfiles,outnfiles,nfiles,ios,ierr character(len=256) :: key,k_value character(len=256),dimension(12) :: input_files,output_files character, dimension(:),allocatable :: par !动态分配命令行参数 logical :: valid !定义常量 integer :: start_pos=6 integer :: max_files=12 n=iargc()!确定命令行参数数量 if (n.lt.3) then!为什么输入文件为2个的时候也要终止?? call help goto 999 end if allocate(par(n)) !文件计数器的初始化 innfiles=0 outnfiles=0 input_files='' output_files=''!这个可以没有 do i=1,n call getarg(i,par(i))!读取command命令行里的输入信息(解析命令行参数) k1 = index(par,'=') if(k1<=0) then print*,"警告:无效的输入格式",trim(par) call help stop end if key = par(1:k1-1)!寻找记录classi_old/newort k_value=par(k1+1:)!用户自命名的classi_old/newort !动态识别文件类型(old/new),并进行名称的存储 if(index(key,'_oldort')>0) then !输入文件的处理 !提取文件编号,读取两位数字 read(key(start_pos:start_pos+1),'(I2)',iostat=ios)j if (ios /=0) then!尝试读取一位数字 read(key(start_pos:start_pos),'(I2)',iostat=ios)j if(ios/=0) then print *, "警告:提取文件编号失败" stop end if end if !存储输入文件名称,并记录输入文件的数量 if(j>=1 .and. j<=max_files) then innfiles=innfiles+1 input_files(innfiles)=trim(k_value) else print*,"警告:无效的文件编码",trim(key) end if else if(index(key,'_newort')>0) then !输出文件的处理 !提取文件编号,读取两位数字 read(key(start_pos:start_pos+1),'(I2)',iostat=ios)j if (ios /=0) then!尝试读取一位数字 read(key(start_pos:start_pos),'(I2)',iostat=ios)j if(ios/=0) then print *, "警告:提取文件编号失败" stop end if end if !存储输出文件名称,并记录输出文件的数量 if(j>=1 .and. j<=12) then outnfiles=outnfiles+1 output_files(outnfiles)=trim(k_value) else print*,"警告:无效的文件编码",trim(key) end if end if end do ! 验证文件配对,查看是否满足一新一旧的匹配 valid = .true. do i = 1, innfiles if (len_trim(input_files(i)) == 0) then!计算字符串的实际长度,忽略尾部的空格 print *, "错误: 缺少输入文件 class", i, "_oldort" valid = .false. end if if (len_trim(output_files(i)) == 0) then print *, "警告: 未指定输出文件 class", i, "_newort, 使用默认值" write(output_files(i), '(a,i0,a)') 'class', i, '_newort.dat' end if end do if (.not. valid .or. innfiles < 2) then print *, "错误: 文件配置无效" call help stop end if nfiles=innfiles ! 调用分类处理子程序 call ort_pick_compare(nfiles,input_files(1:nfiles),output_files(1:nfiles)) deallocate(par(n)) 999 end
07-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值