Fortran 95,如何从行尾读取空白(字母空格)字符?

问题描述 投票:0回答:1

我尝试修改我的程序以读取行尾包含空白字符(用空格书写)的行。我正在使用英特尔编译器(Fortran 95)。

在程序中我使用命令行:

character*(*) line  !this is the character line to where we read

...

...

read (unit=11, '(A80)') line

因此在文件(*.txt-file)中,该行是:

R=

该行的最后一个字符是空白字符(空格)。

有什么方法可以查出该行有3个字符长并且最后一个字符是空白吗?

谢谢!

有什么方法可以查出该行有3个字符长并且最后一个字符是空白吗?

我刚刚得到字符数组:'R=',最后有很多空白字符。

arrays fortran character fortran90 intel-fortran
1个回答
0
投票

好吧,您可以在读取语句中使用

size=
说明符,但这是非常人为的。

以下代码分配(然后重新分配)一个名为 line 的可分配字符。

program test
   implicit none
   character(len=:), allocatable :: line
   integer :: un = 10

   open( un, file="test.dat" )
   line = read_one_line( un );   print *, "["//line//"]", len( line )
   line = read_one_line( un );   print *, "["//line//"]", len( line )
   close( un )

contains
   function read_one_line( u )
      character(len=:), allocatable :: read_one_line
      integer, intent(in) :: u
      character(len=1000) buffer
      integer chars_read

      read( u, "(a)", advance="no", eor=100, size=chars_read ) buffer
 100  read_one_line = buffer(1:chars_read)
   end function read_one_line
end program test

对于包含行的数据文件 test.dat

one 
two  

(每行末尾有必要数量的空格)它生成,方括号 [ 和 ] 只是为了指示行的大小,

 [one ]           4
 [two  ]           5
© www.soinside.com 2019 - 2024. All rights reserved.