用Fortran中的空格读取字符串 [英] Reading a string with spaces in Fortran

查看:710
本文介绍了用Fortran中的空格读取字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果要从用户读取的字符串包含空格,在Fortran中使用read(星号,星号)似乎不起作用。
请考虑以下代码:

  character(Len = 1000):: input =''
read (*,*)输入

如果用户输入字符串Hello,我的名字是John Doe ,只有你好,将被存储在输入中;在空间之后的一切都被忽视了。我的假设是,编译器假设Hello是第一个参数,my是第二个,为了捕获其他单词,我们必须使用类似于read()input1,input2,input3 ...等。这种方法的问题是,我们需要为每个输入创建大字符数组,并且需要确切地知道将输入多少个单词。有没有办法解决??一些函数会实际读取整个句子,空格和全部?非常感谢!

解决方案

character(100):: line

write(*,'(Enter some text:,\)')
read(*,'(A)')line
write(*,'(A)')line

end

...将读取最大长度的文本行100(足够用于大多数实际目的)并将其写回给您。修改您的喜好。


Using read(asterisk, asterisk) in Fortran doesn't seem to work if the string to be read from the user contains spaces. Consider the following code:

    character(Len = 1000) :: input = ' '
    read(*,*) input

If the user enters the string "Hello, my name is John Doe", only "Hello," will be stored in input; everything after the space is disregarded. My assumption is that the compiler assumes that "Hello," is the first argument, and that "my" is the second, so to capture the other words, we'd have to use something like read(,) input1, input2, input3... etc. The problem with this approach is that we'd need to create large character arrays for each input, and need to know exactly how many words will be entered. Is there any way around this?? Some function that will actually read the whole sentence, spaces and all? Many thanks!

解决方案

  character(100) :: line

  write(*,'("Enter some text: ",\)')
  read(*,'(A)') line
  write(*,'(A)') line

  end

... will read a line of text of maximum length 100 (enough for most practical purposes) and write it out back to you. Modify to your liking.

这篇关于用Fortran中的空格读取字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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