在fortran中返回未知长度的字符串 [英] Returning character string of unknown length in fortran

查看:275
本文介绍了在fortran中返回未知长度的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于相当简单的问题我抱歉,我似乎无法找到任何好的Fortran文档。



我试图编写一个从单元,修剪输出并附加空终止符,如下所示:

 字符(*)函数readCString()
字符* 512 str
read(1,*)str
readCString = TRIM(str)// char(0)
返回
结束函数readCString

但是,我知道这不能编译。分段错误最近还不是我的朋友。如果没有函数关键字之前的字符(*),它将不会编译,并且任何值代替明星也会中断,这很可能是因为:

  TRIM(str)// char(0)

不是与我放置的星号相同的长度。
我对Fortran非常陌生,但我试图将一些Fortran代码与C进行接口(因此为空终止符)。

感谢您的帮助。 p>

解决方案

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



以下是一个用作Fortran的方法:

  module test_func_mod 
包含
函数readCString()
使用iso_c_binding
字符(kind = c_char,len = 512):: readCString
字符(kind = c_char,len = 511) :: str
read(1,*)str
readCString = TRIM(str)// C_NULL_CHAR
返回
结束函数readCString

结束模块test_func_mod

程序测试

使用test_func_mod

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

结束程序测试

如果你想要接口Fortran和C,我建议使用新的ISO C绑定。它提供了一种标准的,因此便于连接Fortran和C或任何使用C传递约定的语言的方法。它比过去使用的黑客更容易使用,也不太可能破解。虽然正式成为Fortran 2003的一部分,但它已被众多编译器支持。字符串过去特别困难,一些编译器具有隐藏长度参数等等。ISO C Binding使程序员对所有这些都是透明的。我记得在stackoverflow上给出了一些类似问题的代码(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.

解决方案

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.

这篇关于在fortran中返回未知长度的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆