在FORTRAN中创建名称包含实数的目录

在FORTRAN中创建名称包含实数的目录

本文介绍了在FORTRAN中创建名称包含实数的目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的程序中,我需要为不同情况存储结果文件。我决定创建单独的目录来存储这些结果文件。在这里解释确切的情况是一个伪代码。

  

system 的参数需要是一个字符串。因此,您必须将 real 转换为字符串,并将 mkdir out / 与该字符串连接。这里有一个简单的例子:

pre $ 模块目录
包含
函数dirname(数字)
real,intent(in):: number
character(len = 6):: dirname

!使用6位数字和
将(舍入)数字转换为字符串!前导零
写(d​​irname,'(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 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 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 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.) ) ) )

这篇关于在FORTRAN中创建名称包含实数的目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 10:24