在fortran中返回未知长度的字符串
对于这个相当简单的问题,我深表歉意,我似乎找不到任何好的 fortran 文档。
我正在尝试编写一个从单元读取、修剪输出并附加空终止符的函数,例如:
character(*) function readCString()
character*512 str
read(1, *) str
readCString = TRIM(str)//char(0)
return
end function readCString
但是,我知道这还不能编译。分段错误最近不再是我的朋友。如果函数关键字之前没有“character(*)”,它将无法编译,并且用任何值代替星号它也会中断,很可能是因为: 与
TRIM(str)//char(0)
我代替星号的数字的长度不同。 我对 Fortran 很陌生,但正在尝试将一些 Fortran 代码与 C 接口(因此是空终止符)。
感谢您的任何帮助。
Apologies for the rather simple question, I just can't seem to find ANY good fortran docs.
I'm trying to write a function that reads from a unit, trims the output and appends the null terminator, something like:
character(*) function readCString()
character*512 str
read(1, *) str
readCString = TRIM(str)//char(0)
return
end function readCString
However, I KNOW this doesn't work yet it compiles. Segmentation faults have not been my friend recently. Without the "character(*)" before the function keyword it will not compile, and with any value in place of the star it also breaks, most likely because:
TRIM(str)//char(0)
is not the same length as the number I put in place of the star.
I'm very new to fortran but am trying to interface some fortran code with C (hence the null terminator).
Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
字符(星号)用作函数返回不是一个好主意。字符(星号)不是动态可变长度字符串——它意味着从调用者处获取长度。因此,我建议要么使用固定长度的字符串作为函数返回,要么使用字符(*)作为子例程参数——但在这种情况下,调用者必须提前保留内存,这只会将固定长度推送给调用者。如果您确实不知道最大长度并且必须有一个动态可变长度字符串,那么有办法做到这一点。有一个可变长度字符串模块。 Fortran 2003 具有可分配的缩放器,但并未广泛实现。您可以使用单个字符的数组——可分配数组已经存在很长时间了。
下面是一种适用于 Fortran 的方法:
如果您想连接 Fortran 和 C,我建议使用新的 ISO C 绑定。它提供了一种标准的、因此可移植的方法来连接 Fortran 和 C 或任何使用 C 传递约定的语言。与过去必须使用的 hack 相比,它更容易使用,并且不太可能被破坏。虽然它是 Fortran 2003 的正式组成部分,但它已经得到众多编译器的支持。字符串曾经特别困难,一些编译器具有隐藏的长度参数等。ISO C 绑定使所有这些对程序员来说都是透明的。我记得在 stackoverflow 上给出了类似问题(Fortran、C 和字符串)的一些示例代码,但我找不到该问题。
character(star) is not a good idea to use as a function return. character(star) is not a dynamic variable length string -- it means obtain the length from the caller. So I suggest either using a fixed length string as the function return, or using character(*) as a subroutine argument -- but in that case the caller has to reserve the memory in advance, which just pushes fixing the length to the caller. If you truly don't know the maximum length and must have a dynamicvariable length string, there are way to do it. There is a variable length string module. Fortran 2003 has allocatable scalers, though this isn't widely implemented. You could use an array of single characters -- allocatable arrays have been around a long time.
Here is an method that works as Fortran:
If you want to interface Fortran and C, I recommend using the new ISO C Binding. It provides a standard and therefore portable method of interfacing Fortran and C or any language that uses C passing conventions. It is easier to use and less likely to break than the hacks that had to be used in the past. While officially part of Fortran 2003, it is already supported by numerous compilers. Strings used to be particularly difficult, with some compilers having hidden length arguments, etc. The ISO C Binding makes all of this transparent to the programmer. I remember giving some example code for a similar question (Fortran, C, and strings) on stackoverflow, but I can't find the question.