在fortran中返回未知长度的字符串

发布于 2024-09-07 03:17:32 字数 488 浏览 2 评论 0原文

对于这个相当简单的问题,我深表歉意,我似乎找不到任何好的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心清如水 2024-09-14 03:17:32

字符(星号)用作函数返回不是一个好主意。字符(星号)不是动态可变长度字符串——它意味着从调用者处获取长度。因此,我建议要么使用固定长度的字符串作为函数返回,要么使用字符(*)作为子例程参数——但在这种情况下,调用者必须提前保留内存,这只会将固定长度推送给调用者。如果您确实不知道最大长度并且必须有一个动态可变长度字符串,那么有办法做到这一点。有一个可变长度字符串模块。 Fortran 2003 具有可分配的缩放器,但并未广泛实现。您可以使用单个字符的数组——可分配数组已经存在很长时间了。

下面是一种适用于 Fortran 的方法:

module test_func_mod
contains
function readCString()
  use iso_c_binding
  character (kind=c_char, len=512)  :: readCString
  character (kind=c_char, len=511) :: str
  read(1, *) str
  readCString = TRIM(str) // C_NULL_CHAR
  return
end function readCString

end module test_func_mod

program test

use test_func_mod

open (unit=1, file="temp.txt")
write (*, *) readCString()

end program test

如果您想连接 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:

module test_func_mod
contains
function readCString()
  use iso_c_binding
  character (kind=c_char, len=512)  :: readCString
  character (kind=c_char, len=511) :: str
  read(1, *) str
  readCString = TRIM(str) // C_NULL_CHAR
  return
end function readCString

end module test_func_mod

program test

use test_func_mod

open (unit=1, file="temp.txt")
write (*, *) readCString()

end program test

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文