Convert integers to strings to create output filenames at run time

前端 未结 9 2005
小鲜肉
小鲜肉 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: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
    

提交回复
热议问题