将不同长度的字符串传递给Fortran中的函数 [英] Passing character strings of different lengths to functions in Fortran
问题描述
我正在将Fortran 90与gfortran编译器一起使用,作为cygwin的一部分.我想编写一个函数,该函数将在目录中创建一系列新文件夹,该目录也作为参数传递,该数字是新连续编号文件夹的最大数量.因为我必须声明字符(即字符串)的长度,而且还希望通用地能够传递不同的路径,所以我尝试将修剪后的字符串传递给函数.
I am using Fortran 90 with the gfortran compiler as part of cygwin. I want to write a function that will create a series of new folders into a directory that is also passed as a parameter along with a number that is the maximum number of new consecutively numbered folders. Since I have to declare the length of the characters (ie strings) but also want to universally be able to pass different paths, I tried to pass the trimmed strings to the function.
program main
implicit none
character(len = 6) :: newdir
character(len = 27) :: path
newdir = "neu1A"
path = "c:/users/i/desktop/rainer"
print*,len_trim(path) !Outputs the correct length of 25
print*,len_trim(newdir) !Outputs the correct length of 5
call newdirec(trim(newdir),trim(path),5)
end program main
但是由于我必须在函数中重新声明参数,因此在过程中它们的长度将被覆盖/丢失.我该如何使用正确的字符串长度并保持该函数的通用性?我必须使用这些长度,因为需要使用格式化字符串来构建调用系统来创建目录的字符串.我使用的是Fortran 90,所以一些选项不可用.
But since I have to newly declare the parameters in the function, their length is overwritten/lost in the process. How can I use the correct length of the strings and keep the functional usable universally? I have to use the lengths because of the formatting string needed for building a string that calls onto the system to create the directories. I use Fortran 90, so a few options are not available.
function newdirec(newdir,path, foldnum)
character (len = 27) :: path
character (len = 50) :: newdir
character (len = (len_trim(path) + len_trim(newdir))) :: newpath
character (len = 100) :: format_string, newdir_len_str, makedir
integer :: foldnum
newpath = trim(path)//"/"//trim(newdir)
print*,len_trim(newpath) !Outputs the 'wrong' but declared length of 77
write(newdir_len_str, "(I2)") len_trim(newpath)
do i = 1, foldnum
if (i < 10) then
format_string = "(A"//trim(newdir_len_str)//",I1)"
elseif (i < 100) then
format_string = "(A"//trim(newdir_len_str)//",I2)"
else
format_string = "(A"//trim(newdir_len_str)//",I3)"
endif
write (makedir, format_string) "mkdir "//trim(newpath),i
!call system(trim(makedir))
print *, trim(makedir)
end do
return
end function newdirec
推荐答案
正如弗拉基米尔(Vladimir)所建议的那样,声明子例程或函数的字符参数的常规方法是:
As Vladimir suggested, the normal way to declare character arguments of a subroutine or function is :
function newdirec(newdir,path, foldnum)
character (*) :: newdir,path
...
在这种情况下,内在函数LEN允许您获取从调用过程继承的字符串的大小.
In that case, the intrinsic function LEN allows you to get the size of the strings inherited from the calling procedure.
我绝对没有充分的理由声明它们的固定长度,例如27或50 ...,除非您确实在不久的将来遇到麻烦,例如,当您将在调用过程中更改声明时:不匹配通常会导致难以理解的奇怪致命错误.
I see absolutely no good reason to declare them with a fixed length like 27 or 50 ... except if you really look for troubles in a near future, for instance when you will change the declarations in the calling procedure : a length mismatch generally leads to a weird fatal error difficult to understand.
这篇关于将不同长度的字符串传递给Fortran中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!