Fortran的点点滴滴——批量生成文件
设要生成4000个文件,文件名为AA1-AA4000,如何写循环生成文件,而不用写4000次write命令呢?!这时可以用内部文件来实现该功能:
PROGRAM MAIN CHARACTER(LEN=80) :: FILENAME,FORM INTEGER :: I DO I=1,4000 SELECT CASE (I)
CASE (1:9) WRITE(FORM,'(I1)') I CASE (10:99) WRITE(FORM,'(I2)') I CASE (100:999) WRITE(FORM,'(I3)') I CASE (1000:9999) WRITE(FORM,'(I4)') I END SELECT WRITE(FILENAME,*) "AA",TRIM(FORM),".TXT" OPEN(10,FILE=FILENAME) WRITE(10,*) I CLOSE(10) END DO END PROGRAM
上面程序中字符变量FORM、FILENAME充当了内部文件,用来存储临时内容,通过格式化输出,可以对其内容灵活控制。