CHARACTER*3 STR
WRITE(STR,'(A3)')123
此时STR作为内部文件使用
: name you want to this string (like sprintf in C), and use the string.
: character(len=12)::fname
: integer::i
: do i=0,9999,1
: write(unit=fname,fmt="('file',i4.4,'.dat')")i ! like sprintf() in C
: open(unit=10,file=fname)
: ...
: close(unit=10)
: end do
file001.txt, file002.txt, ...
贴出源程序,供各位参考:
文件名: NAMEGEN.F90
****************************************************************************
MODULE NAMEGEN_
CONTAINS
! *****************************************************************
FUNCTION NAMEGEN (NAME, NUM, INDEX, EXT)
! *****************************************************************
!
! 程序说明:
! Generate sorted file name. Attach the number after each filename.
! The conversion of integer value into character is used.
!
! INPUT:
! NAME filename;
! NUM indicate the maximum file number
! used, e.g.
! "0000","00000";
! INDEX the number of this file;
! EXT extension of the file;
! EXAMPLE:
! NAME='FILE'
! NUM='0000'
! INDEX=18
! EXT='TXT'
! Returned name is: FILE0018.TXT
!
! Xin Zhao, 2002-2-12
! *****************************************************************
CHARACTER*108 NAMEGEN,NA,NUM1
CHARACTER*(*) NAME,EXT,NUM
INTEGER*4 INDEX,LENGTH1,LENGTH2
WRITE(NA,'(I4)') INDEX
NA=ADJUSTL (NA)
LENGTH1=LEN_TRIM(NA)
LENGTH2=LEN_TRIM(NUM)
NUM1=NUM(1:(LENGTH2-LENGTH1))//NA
NAMEGEN=TRIM(NAME)//TRIM(NUM1)//'.'//TRIM(EXT)
END FUNCTION
END MODULE
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.