Convert integers to strings to create output filenames at run time

前端 未结 9 2011
小鲜肉
小鲜肉 2020-11-22 02:47

I have a program in Fortran that saves the results to a file. At the moment I open the file using

OPEN (1, FILE = \'Output.TXT\')

However,

相关标签:
9条回答
  • 2020-11-22 03:29

    A much easier solution IMHO ...................

    character(len=8) :: fmt ! format descriptor
    
    fmt = '(I5.5)' ! an integer of width 5 with zeros at the left
    
    i1= 59
    
    write (x1,fmt) i1 ! converting integer to string using a 'internal file'
    
    filename='output'//trim(x1)//'.dat'
    
    ! ====> filename: output00059.dat
    
    0 讨论(0)
  • 2020-11-22 03:29

    To convert an integer to a string:

    integer :: i    
    character* :: s    
    if (i.LE.9) then
         s=char(48+i)    
    else if (i.GE.10) then
         s=char(48+(i/10))// char(48-10*(i/10)+i)    
    endif
    
    0 讨论(0)
  • 2020-11-22 03:30

    I've tried @Alejandro and @user2361779 already but it gives me an unsatisfied result such as file 1.txt or file1 .txt instead of file1.txt. However i find the better solution:

    ...
    integer :: i
    character(len=5) :: char_i     ! use your maximum expected len
    character(len=32) :: filename
    
    write(char_i, '(I5)') i        ! convert integer to char
    write(filename, '("path/to/file/", A, ".dat")') trim(adjustl(char_i))
    ...
    

    Explanation:

    e.g. set i = 10 and write(char_i, '(I5)') i

    char_i                gives  "   10" ! this is original value of char_i
    
    adjustl(char_i)       gives  "10   " ! adjust char_i to the left
    
    trim(adjustl(char_i)) gives  "10"    ! adjust char_i to the left then remove blank space on the right
    

    I think this is a simplest solution that give you a dynamical length filename without any legacy blank spaces from integer to string conversion process.

    0 讨论(0)
  • 2020-11-22 03:39

    For a shorten version. If all the indices are smaller than 10, then use the following:

    do i=0,9
       fid=100+i
       fname='OUTPUT'//NCHAR(i+48) //'.txt'
       open(fid, file=fname)
       !....
    end do
    

    For a general version:

    character(len=5) :: charI
    do i = 0,100
       fid = 100 + i
       write(charI,"(A)"), i
       fname ='OUTPUT' // trim(charI) // '.txt'
       open(fid, file=fname)
    end do
    

    That's all.

    0 讨论(0)
  • 2020-11-22 03:42

    Well here is a simple function which will return the left justified string version of an integer:

    character(len=20) function str(k)
    !   "Convert an integer to string."
        integer, intent(in) :: k
        write (str, *) k
        str = adjustl(str)
    end function str
    

    And here is a test code:

    program x
    integer :: i
    do i=1, 100
        open(11, file='Output'//trim(str(i))//'.txt')
        write (11, *) i
        close (11)
    end do
    end program x
    
    0 讨论(0)
  • 2020-11-22 03:42

    Here is my subroutine approach to this problem. it transforms an integer in the range 0 : 9999 as a character. For example, the INTEGER 123 is transformed into the character 0123. hope it helps.

    P.S. - sorry for the comments; they make sense in Romanian :P

     subroutine nume_fisier (i,filename_tot)
    
       implicit none
       integer :: i
    
       integer :: integer_zeci,rest_zeci,integer_sute,rest_sute,integer_mii,rest_mii
       character(1) :: filename1,filename2,filename3,filename4
       character(4) :: filename_tot
    
    ! Subrutina ce transforma un INTEGER de la 0 la 9999 in o serie de CARACTERE cu acelasi numar
    
    ! pentru a fi folosite in numerotarea si denumirea fisierelor de rezultate.
    
     if(i<=9) then
    
      filename1=char(48+0)
      filename2=char(48+0)
      filename3=char(48+0)
      filename4=char(48+i)  
    
     elseif(i>=10.and.i<=99) then
    
      integer_zeci=int(i/10)
      rest_zeci=mod(i,10)
      filename1=char(48+0)
      filename2=char(48+0)
      filename3=char(48+integer_zeci)
      filename4=char(48+rest_zeci)
    
     elseif(i>=100.and.i<=999) then
    
      integer_sute=int(i/100)
      rest_sute=mod(i,100)
      integer_zeci=int(rest_sute/10)
      rest_zeci=mod(rest_sute,10)
      filename1=char(48+0)
      filename2=char(48+integer_sute)
      filename3=char(48+integer_zeci)
      filename4=char(48+rest_zeci)
    
     elseif(i>=1000.and.i<=9999) then
    
      integer_mii=int(i/1000)
      rest_mii=mod(i,1000)
      integer_sute=int(rest_mii/100)
      rest_sute=mod(rest_mii,100)
      integer_zeci=int(rest_sute/10)
      rest_zeci=mod(rest_sute,10)
      filename1=char(48+integer_mii)
      filename2=char(48+integer_sute)
      filename3=char(48+integer_zeci) 
      filename4=char(48+rest_zeci)
    
     endif
    
     filename_tot=''//filename1//''//filename2//''//filename3//''//filename4//''
     return
     end subroutine nume_fisier
    
    0 讨论(0)
提交回复
热议问题