分类:
2009-02-27 13:37:52
整型转字符,借助FORTRAN语言的内部文件完成,即将一个字符串变量当作一个内部文件来看待。给一个实用的例子吧。 假如你有一组文件有20个,命名规律是myFile*.dat,其中*是从1到20递增的整型数,则要用循环依次打开这些文件可以这样写: program main implicit none character( len = 2 ) :: cTemp integer :: k do k = 1, 20 write( cTemp,'(i2)' ) k open ( 1, file = 'myFile' // trim(adjustl( cTemp )) // '.dat', status = 'old' ) ... close( 1 ) end do end porgram main 这里因为k可能是一位数,也可能是两位数,所以字符串变量cTemp的长度至少要是2个字符长度,才能保证最大整数能装下。trim和adjustl是FORTRAN的标准内部函数。adjustl的作用是将字符串里的内容左对齐,空格置于右端。trim的作用是将字符串末尾(即右端)的空格删掉。这样无论你的k是一位数还是两位数,都可以保证open路径中不会出现多余的空格。// 是FORTRAN的字符串操作符,作用是将字符串连接起来。 同理,如果是字符型数字转整型或实型,方法一样,比如: character( len = 4 ) :: cTemp = '2007' integer :: year read( cTemp, '(i4)' ) :: year %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
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.