Chinaunix首页 | 论坛 | 博客
  • 博客访问: 57674
  • 博文数量: 13
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 151
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-10 15:31
文章分类

全部博文(13)

文章存档

2014年(13)

我的朋友

分类: LINUX

2014-04-23 16:50:22

FORTRAN语言中整数转换为字符串,如整数123变为字符串


整型转字符,借助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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%用write语句  
  CHARACTER*3   STR  
  WRITE(STR,'(A3)')123  
  此时STR作为内部文件使用%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Use a character string variable as an "internal file", write the file
: 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.

阅读(4593) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~