Creating directory with name containing real number in FORTRAN

前端 未结 1 939
长情又很酷
长情又很酷 2021-01-03 10:09

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 her

相关标签:
1条回答
  • 2021-01-03 10:23

    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.) ) ) )
    
    0 讨论(0)
提交回复
热议问题