在我的程序中,我需要存储不同情况的结果文件。我决定创建单独的目录来存储这些结果文件。这里用伪代码来解释具体的情况。
do i=1,N ! N cases of my analysis
U=SPEED(i)
call write_files(U) !Create a new directory for this case and Open files (1 = a.csv, 2 = b.csv) to write data
call postprocess() !Write data in files (a.csv, b.csv)
call close_files() !Close all files (1,2)
end do
subroutine write_files(i)
!Make directory i
!Open file a.csv and b.csv with unit 1 & 2
!Write header information in file a.csv and b.csv
close subroutine
Run Code Online (Sandbox Code Playgroud)
我正在努力将实际变量 U 转换为字符变量,以便我可以用来call system('mkdir out/' trim(U))创建单独的文件夹来存储我的结果。
我还想提一下,我的变量 U 是速度,就像0.00000, 1.00000, 1.50000等。有没有一种方法可以简化我的目录名称,使其像0,1,1.5等。
希望我的解释很清楚。如果不让我知道,我会尝试按要求进行编辑。
谢谢你的帮助。
的参数system必须是一个字符串。因此,您必须将 转换real为字符串并mkdir out/与该字符串连接。这是一个简单的例子:
module dirs
contains
function dirname(number)
real,intent(in) :: number
character(len=6) :: dirname
! Cast the (rounded) number to string using 6 digits and
! leading zeros
write (dirname, '(I6.6)') nint(number)
! This is the same w/o leading zeros
!write (dirname, '(I6)') nint(number)
! This is for one digit (no rounding)
!write (dirname, '(F4.1)') number
end function
end module
program dirtest
use dirs
call system('mkdir -p out/' // adjustl(trim( dirname(1.) ) ) )
end program
Run Code Online (Sandbox Code Playgroud)
您可以使用 Fortran 2008 语句(如果您的编译器支持它) ,而不是call system(...)非标准语句。execute_command_line
call execute_command_line ('mkdir -p out/' // adjustl(trim( dirname(1.) ) ) )
Run Code Online (Sandbox Code Playgroud)