2014年(13)
分类: LINUX
2014-04-23 16:50:22
Fortran字符串操作备忘录——产生序列文件名
2008-2-18 幻影整理
主程序: 根据一个前缀字符串、一个序列号,生成一个文件名。
函数1:根据一个字符串、串定义长度,计算该串非空字符的首个位置。
函数2:根据一个字符串、串定义长度、非空字符的首个位置,计算该串非空字符的未尾位置。
cccccccccccccccccccccccccccccccccccccccccc Main Program
program test
cccc Just to make string with prefix Name
cccc and serial Num
parameter(lengthStr=80,lengthNum =5)
character(len=lengthStr)::str
character(len=lengthNum)::num
character(len=lengthStr)::preName
integer i,baseNum
integer loca1Pre,loca2Pre
integer loca1Num,loca2Num
integer loca1Str,loca2Str
ccccccccccccccccccccccccc make subString : prefix Name
preName='file'
loca1Pre = loca1(preName,lengthStr)
loca2Pre = loca2(preName,lengthStr,loca1Pre)
write(*,*)'preName (',loca1Pre,':',loca2Pre,
+ ') = ', preName(loca1Pre:loca2Pre)
baseNum = 1000 ! 为整型数值转换为长度相同(3位)的字符串而准备
cccccccccccccccccccccccc
do i=1,10
cccc make subString : serial Num
write(num,*)baseNum + i ! 统一整型数的位数,将1变为1001,转字符串类型
loca1Num = loca1(num,lengthNum)+1 !非空字符的首个位置向后移动一位,将1001变为001
loca2Num = loca2(num,lengthNum, loca1Num)
cccc make final String ! 前缀_SerialNum.txt
str = preName(loca1Pre:loca2Pre)//'_'
+ //num(loca1Num:loca2Num)//'.txt'
loca1Str = loca1(str,lengthStr)
loca2Str = loca2(str,lengthStr, loca1Str)
cccc
write(*,*)'i=',i,', num=',num,', num(',loca1Num,':',loca2Num,
+ ')=',num(loca1Num:loca2Num) ,
+ ', str(',loca1Str,':',loca2Str,
+ ' )= ', str(loca1Str:loca2Str),'.'
enddo
end
ccccccccccccccccccccccccccccccccccccccccc Function 1
integer function loca1(str,length)
cccc get the begin point of string
character*256 str
integer loca2,length
integer i
do i=1,length
if (str(i:i).ne.'') then
goto 222
endif
enddo
222 loca1=i
return
end
ccccccccccccccccccccccccccccccccccccccccc Function 2
integer function loca2(str,length,loca1)
cccc get the end point of string
character*256 str
integer loca1 ,length
do i=loca1+1,length
if (str(i:i).eq.'') then
goto 223
endif
enddo
223 loca2 = i-1
return
end
ccccccccccccccccccccccccccccccccccccccccccc
运行结果:
preName ( 1: 4) = file
i= 1, num= 1001, num( 3: 5)=001, str( 1: 12 )= file_001.txt.
i= 2, num= 1002, num( 3: 5)=002, str( 1: 12 )= file_002.txt.
i= 3, num= 1003, num( 3: 5)=003, str( 1: 12 )= file_003.txt.
i= 4, num= 1004, num( 3: 5)=004, str( 1: 12 )= file_004.txt.
i= 5, num= 1005, num( 3: 5)=005, str( 1: 12 )= file_005.txt.
i= 6, num= 1006, num( 3: 5)=006, str( 1: 12 )= file_006.txt.
i= 7, num= 1007, num( 3: 5)=007, str( 1: 12 )= file_007.txt.
i= 8, num= 1008, num( 3: 5)=008, str( 1: 12 )= file_008.txt.
i= 9, num= 1009, num( 3: 5)=009, str( 1: 12 )= file_009.txt.
i= 10, num= 1010, num( 3: 5)=010, str( 1: 12 )= file_010.txt.