使用延迟长度字符串读取用户输入 [英] Using a deferred-length character string to read user input

查看:13
本文介绍了使用延迟长度字符串读取用户输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以简单"的方式使用延迟长度的字符串来读取用户输入.我想这样做的原因是我不想在知道用户输入有多大之前必须声明字符串的大小.我知道有复杂"的方法可以做到这一点.例如,可以使用 iso_varying_string 模块:https://www.fortran.com/iso_varying_string.f95.另外,这里有一个解决方案:Fortran Character Input at Undefined Length.但是,我希望有一些简单或几乎一样简单的东西,如下所示:

I would like to use deferred-length character strings in a "simple" manner to read user input. The reason that I want to do this is that I do not want to have to declare the size of a character string before knowing how large the user input will be. I know that there are "complicated" ways to do this. For example, the iso_varying_string module can be used: https://www.fortran.com/iso_varying_string.f95. Also, there is a solution here: Fortran Character Input at Undefined Length. However, I was hoping for something as simple, or almost as simple, as the following:

program main

  character(len = :), allocatable :: my_string
  read(*, '(a)') my_string
  write(*,'(a)') my_string
  print *, allocated(my_string), len(my_string)

end program

当我运行这个程序时,输出是:

When I run this program, the output is:

./a.out
here is the user input

F       32765

请注意,write(*,'(a)') my_string 没有输出.为什么?

Notice that there is no output from write(*,'(a)') my_string. Why?

另外,my_string 尚未分配.为什么?

Also, my_string has not been allocated. Why?

为什么这不是 Fortran 的一个简单功能?其他语言有这个简单的功能吗?我一般对这个问题缺乏一些基本的了解吗?

Why isn't this a simple feature of Fortran? Do other languages have this simple feature? Am I lacking some basic understanding about this issue in general?

推荐答案

vincentjs的回答不太对.

vincentjs's answer isn't quite right.

现代 (2003+) Fortran 确实允许在赋值时自动分配和重新分配字符串,因此诸如此类的一系列语句

Modern (2003+) Fortran does allow automatic allocation and re-allocation of strings on assignment, so a sequence of statements such as this

character(len=:), allocatable :: string
...
string = 'Hello'
write(*,*)
string = 'my friend'
write(*,*)
string = 'Hello '//string
write(*,*)

是正确的,将按预期工作并写出 3 个不同长度的字符串.至少有一个广泛使用的编译器,英特尔 Fortran 编译器,默认情况下不使用 2003 语义,因此在尝试编译时可能会引发错误.有关使用 Fortran 2003 的设置,请参阅文档.

is correct and will work as expected and write out 3 strings of different lengths. At least one compiler in widespread use, the Intel Fortran compiler, does not engage 2003 semantics by default so may raise an error on trying to compile this. Refer to the documentation for the setting to use Fortran 2003.

但是,在读取字符串时,此功能不可用,因此您必须采用经过试验和测试(如果您愿意,aka 是老式的)方法来声明一个足够大的缓冲区以供任何输入,然后分配可分配变量.像这样:

However, this feature is not available when reading a string so you have to resort to the tried and tested (aka old-fashioned if you prefer) approach of declaring a buffer of sufficient size for any input and of then assigning the allocatable variable. Like this:

character(len=long) :: buffer
character(len=:), allocatable :: string
...
read(*,*) buffer
string = trim(buffer)

不,我不知道为什么语言标准禁止在 read 上自动分配,只是确实如此.

No, I don't know why the language standard forbids automatic allocation on read, just that it does.

这篇关于使用延迟长度字符串读取用户输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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