Fortran中的可分配字符变量 [英] Allocatable character variables in Fortran

查看:943
本文介绍了Fortran中的可分配字符变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码(分为我认为与此问题相关的)是

  PROGRAM测试

IMPLICIT NONE

CHARACTER(len = 37)input
CHARACTER(len = :),allocatable :: input_trim


WRITE ,*)'文件名?'
输入
ALLOCATE(字符(len = LEN(TRIM(输入))):: input_trim





END PROGRAM测试

它适用于Intel的Fortran编译器,不过gfortran给我一些错误,第一个在行

  CHARACTER(len = :),allocatable :: input_trim 



我不知道哪个编译器对于Fortran标准是正确的。加上我不知道如何实现我需要一个不同的方式?我想我做的更多是一个解决方法。我需要的是一个字符变量,其中包含的输入文件名没有下面的空格。



编辑:错误是CHARACTER声明中的语法错误。
gfortran --version给我GNU Fortran(GCC)4.4.7 20120313(Red Hat 4.4.7-3)



编辑2: re right关于分配:有了ifort,我不需要它。 gfortran崩溃之前,所以也许它不需要分配,但我不能测试这一刻...

解决方案

 字符(len = :),allocatable :: input_trim 
pre>

当然在Fortran 2003中语法正确。你不说什么错误, gfortran 所以我不能评论为什么它不接受行 - 也许你有一个旧版本的编译器安装。



有了最新的Fortran编译器(例如 Intel Fortran v14.xxx)在分配字符变量之前不需要分配字符变量的大小,您只需写

  input_trim = trim(input)

/ p>

 阅读(*,*)input_trim 

无效。


My code (stripped down to what I think is relevant for this question) is

PROGRAM test

IMPLICIT NONE

CHARACTER(len=37) input
CHARACTER(len=:), allocatable :: input_trim


WRITE(*,*) 'Filename?'
READ(*,*) input
ALLOCATE(character(len=LEN(TRIM(input))) :: input_trim)
input_trim=trim(input)

.
.
.

END PROGRAM test

It works fine with Intel's Fortran compiler, however gfortran gives me a couple of errors, the first one being in the line saying

CHARACTER(len=:), allocatable :: input_trim

I'm not sure which compiler is 'right' regarding the Fortran standard. Plus I don't know how to achieve what I need in a different way?! I think what I'm doing is more of a workaround anyway. What I need is a character variable containing exactly the filename that was entered with no following spaces.

EDIT: The error is "Syntax error in CHARACTER declaration". gfortran --version gives me "GNU Fortran (GCC) 4.4.7 20120313 (Red Hat 4.4.7-3)"

EDIT 2: You're right regarding the allocate: With ifort, I don't need it. And gfortran crashes before that so maybe it doesn't need the allocate either but I cannot test this at the moment...

解决方案

This

character (len=:), allocatable :: input_trim

is certainly syntactically correct in Fortran 2003. You don't say what the error that gfortran raises is, so I can't comment on why it doesn't accept the line -- perhaps you have an old version of the compiler installed.

With an up-to-date Fortran compiler (eg Intel Fortran v14.xxx) you don't need to allocate the character variable's size prior to assigning to it, you can simply write

input_trim = trim(input)

Note that

read(*,*) input_trim

won't work.

这篇关于Fortran中的可分配字符变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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