我是新的Fortran用户。我正在尝试根据日期和时间创建文件名。我只知道打开fie的命令是:
open (unit=10,file='test.txt')
我想使用程序执行时的当前日期和时间来使用文件名而不是'test.txt'
。如果有人可以帮助我,那么我将不胜感激。
答案 0 :(得分:1)
您可以使用date_and_time
来实现此目标:
module time_ftcs
contains
function timestamp() result(str)
implicit none
character(len=20) :: str
integer :: values(8)
character(len=4) :: year
character(len=2) :: month
character(len=2) :: day, hour, minute, second
character(len=5) :: zone
! Get current time
call date_and_time(VALUES=values, ZONE=zone)
write(year,'(i4.4)') values(1)
write(month,'(i2.2)') values(2)
write(day,'(i2.2)') values(3)
write(hour,'(i2.2)') values(5)
write(minute,'(i2.2)') values(6)
write(second,'(i2.2)') values(7)
str = year//'-'//month//'-'//day//'_'&
//hour//':'//minute//':'//second
end function timestamp
end module
program test
use time_ftcs, only: timestamp
open (unit=10,file='test'//trim(timestamp())//'.txt')
write(10,*) 'Hello World'
close(10)
end program
这会产生一个文件
$cat test2015-04-05_09:32:27.txt
Hello World
答案 1 :(得分:0)
您可以使用内在子例程date_and_time
来实现此目的:
module time_ftcs
contains
function timestamp() result(str)
implicit none
character(len=15) :: str
character(len=8) :: dt
character(len=10) :: tm
! Get current time
call date_and_time(DATE=dt, TIME=tm)
str = dt//'_'//tm(1:6) ! tm(7:10) are milliseconds and decimal point
end function timestamp
end module
program test
use time_ftcs, only: timestamp
open (unit=10,file='test_'//timestamp//'.txt')
write(10,*) 'Hello World'
close(10)
end program
这应该会产生一个文件
$cat test_20150405_093227.txt
Hello World