Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Fortran处理空空间的方式_Fortran_Fortran90 - Fatal编程技术网

Fortran处理空空间的方式

Fortran处理空空间的方式,fortran,fortran90,Fortran,Fortran90,我想澄清一下Fortran处理字符串中“空”字符的方式。 让我们假设我们有这种情况: program main implicit none test('AB') end program 在哪里 我对这件事很好奇。 提前感谢。Fortran中的标准字符串是固定长度的。如果不使用整个字符串,则会在其末尾填充空格/空格 我修改了您的示例程序,以通过gfortran和ifort的编译器检查。您的函数没有返回,因此作为子例程更好。编译器注意到实际参数和伪参数的长度不一致——因为我将过程放入一个模块中,

我想澄清一下Fortran处理字符串中“空”字符的方式。 让我们假设我们有这种情况:

program main
implicit none

test('AB')
end program
在哪里

我对这件事很好奇。
提前感谢。

Fortran中的标准字符串是固定长度的。如果不使用整个字符串,则会在其末尾填充空格/空格

我修改了您的示例程序,以通过gfortran和ifort的编译器检查。您的函数没有返回,因此作为子例程更好。编译器注意到实际参数和伪参数的长度不一致——因为我将过程放入一个模块中,并
使用
对其进行编辑,以便编译器可以检查参数的一致性。他们抱怨将长度为2的字符串传递给长度为10的字符串。其余的字符应该如何定义

module test_mod

contains

subroutine test(name)
implicit none
character(10) :: name
character(3)  :: cutname

write(*,*) '-'//name//'-'             ! Gives output "-AB        -"
                                      ! Space has then been added at the end
cutname(1:3) = name(1:3)
write(*,*) '1-'//cutname//'-'           ! Gives output "-AB -"
                                        ! It seems there is a space then at the end
                                        ! of cutname

write(*,*)  (cutname(1:2) == 'AB')      ! Gives output  T (true)
write(*,*)  (cutname(3:3) == ' ')       ! Gives output  F (false)
write(*,*)  (cutname  == 'AB ')         ! Gives output  F (false)

end subroutine test

end module test_mod



program main
use test_mod
implicit none

call test('AB          ')
end program
当我运行这个版本时,输出是T、T和T,这就是我所期望的

编辑:我建议使用编译器的完整警告和错误检查选项。这就是我如何快速发现示例中的问题的原因。使用gfortran:
-O2-fimplicit none-Wall-Wline truncation-Wcharacter truncation-Wsurprising-Waliasing-Wimplicit interface-Wunused参数-fwhole文件-fcheck=all-std=f2008-pedantic-fbacktrace


字符串赋值语句不要求两边具有相同的长度。如果RHS比LHS上的字符串变量短,则会在末尾用空格填充。在这里,参数应该是一致的,包括长度

我否决了这个问题,因为发布的代码无法编译。我认为,即使是Fortran也可以在不执行的代码中显示意外行为。如果您执行
'-'//TRIM(name)//'-'
,您将得到想要的结果
TRIM
从字符数组中删除多余的空格。@HighPerformanceMark,我想给出一个示例来解释这个问题,但不显示所有代码。我的代码并没有抱怨字符长度之间的“不一致”,以及我刚才提到的奇怪后果。我花了将近4个小时试图弄明白原因,但我没能弄明白。@SethMMorton Seth,我的案子发生了一些奇怪的事情。就连我也在修改“名字”,我在下面的测试中得到了F的答案。也就是说,这就像“名称”中添加的具有一致字符长度(10)的“尾随空格”被打印为空白,但没有被识别为空。无论如何,让我再做一些检查。我应该很清楚…
'-'//TRIM(name)//'-'
将给出字符串
'-AB-..
(为了清晰起见,点是空格)。如果要与字符串进行比较,则需要执行(例如)
TRIM(cutname)=='AB'
。这将在与
'AB'
进行比较之前修剪
cutname
的额外空白。如果您的字符数组长度为10个字符,并且其中只有两个字符,那么您的末尾将始终有8个空格。您需要每次使用
trim
或切片表示法来去除这些空格,或者制作
cutname
定义它时只有两个字符长。谢谢你的回答。实际上我使用的编译器(默认级别的gfortran)并没有抱怨(我的示例稍微复杂一点,但probem的“内核”正是你文章中显示的那个)。我的子例程在一个模块内(我在main中调用它)。当我从main中调用子例程时,使用不同数量的字符,我得不到正确的答案。@RaunoC.:您总是可以定义
字符(len=*)::name
然后
字符(len=len(name))::cutname
在子例程中,并不真正担心字符长度不一致谢谢你@KyleKanos,这是一个很好的解决方案,因为我一直在理解这种奇怪的行为:-)是的,在花了几个小时试图理解这种奇怪的行为之后,我肯定希望调试器能够解决这个问题。正如您所说,“如果RHS比LHS上的字符串变量短,那么它的末尾将被填充空格。”但是,在我的情况下,这些空格没有被识别为空格。(写(,)name(4)=''给出F)
module test_mod

contains

subroutine test(name)
implicit none
character(10) :: name
character(3)  :: cutname

write(*,*) '-'//name//'-'             ! Gives output "-AB        -"
                                      ! Space has then been added at the end
cutname(1:3) = name(1:3)
write(*,*) '1-'//cutname//'-'           ! Gives output "-AB -"
                                        ! It seems there is a space then at the end
                                        ! of cutname

write(*,*)  (cutname(1:2) == 'AB')      ! Gives output  T (true)
write(*,*)  (cutname(3:3) == ' ')       ! Gives output  F (false)
write(*,*)  (cutname  == 'AB ')         ! Gives output  F (false)

end subroutine test

end module test_mod



program main
use test_mod
implicit none

call test('AB          ')
end program