在我的程序中,我需要为不同情况存储结果文件。我决定创建单独的目录来存储这些结果文件。在这里解释确切的情况是一个伪代码。
do i = 1,N! N分析我的分析 U = SPEED(i)调用write_files(U)!为这种情况创建一个新目录并打开文件(1 = a.csv,2 = b.csv)数据调用postprocess()!将数据写入文件(a.csv,b.csv)调用close_files()!关闭所有文件(1,2) end do 子程序write_files(i)!使目录i !打开文件a.csv和b.csv,单位为1& 2 !写入文件a.csv和b.csv中的标题信息关闭子程序我努力将实际变量U转换为字符变量,以便我可以使用调用system('mkdir out /'trim(U))来创建单独的文件夹来存储我的结果。
我还想提一下,我的变量U是速度,就像 0.00000,1.0000,1.50000 等。有没有一种方法可以简化我的目录名称,使它像 0,1,1.5 等。
希望我的解释很清楚。如果没有告诉我,我会尽量根据需要进行编辑。
感谢您的帮助。
解决方案system 的参数需要是一个字符串。因此,您必须将 real 转换为字符串,并将 mkdir out / 与该字符串连接。这里有一个简单的例子: pre $ 模块目录包含函数dirname(数字) real,intent(in):: number character(len = 6):: dirname !使用6位数字和将(舍入)数字转换为字符串!前导零写(dirname,'(I6.6)')nint(数字)!这是相同的W / O前导零!写(dirname,'(I6)')nint(数字) ! (dirname,'(F4.1)')编号结束函数结束模块 程序dirtyest use dirs call system('mkdir -p out /'// adjustl(trim(dirname(1。)))) end program
代替调用系统(...)这是非标准的,您可以使用Fortran 2008语句 execute_command_line (如果您的编译器支持它的话)。
pre $ 调用execute_command_line('mkdir -p out /'// adjustl(trim(dirname(1。))))
In my program I need to store result files for different cases. I have decided to create separate directories to store these result files. To explain the exact situation here is a pseudo code.
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 subroutineI am struggling in converting the real variable U to a character variable so that I can use call system('mkdir out/' trim(U)) to create separate folders to store my results.
I would also like to mention that my variable U is speed which is like 0.00000, 1.00000, 1.50000 etc. Is there a way I can simplify my directory name so it is like 0,1,1.5 etc.
Hope my explanation is clear. If not let me know, I will try to edit as required.
Thank you for help.
解决方案The argument of system needs to be a string. You therefore have to cast the real to a string and concatenate mkdir out/ with that string. Here is a quick example:
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 programInstead of call system(...) which is non-standard, you could use the Fortran 2008 statement execute_command_line (if your compiler supports it).
call execute_command_line ('mkdir -p out/' // adjustl(trim( dirname(1.) ) ) )