Creating directory with name containing real number in FORTRAN
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 program
Instead 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.) ) ) )
Amitava
I am currently working as a research and development engineer in the marine industry. My research area involves analysis of hydrodynamic performance of floating vessels, which includes boats, ships, offshore platforms and floating wind turbines. The programming languages that come handy are: FORTRAN, Matlab, Python and VBA.
Updated on June 04, 2022Comments
-
Amitava almost 2 years
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 subroutine
I 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 like0,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.