Convert integers to strings to create output filenames at run time

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

提交回复
热议问题