提问人:Mikko T 提问时间:9/11/2023 最后编辑:Vladimir F Героям славаMikko T 更新时间:9/12/2023 访问量:97
Fortran 95,如何从行尾读取空白(字母空格)字符?
Fortran 95, How to read blank (letter space) character from the end of the line?
问:
我尝试修改我的程序以读取行尾包含空白字符(用空格书写)的行。我正在使用英特尔编译器 (Fortran 95)。
在程序中,我使用命令行:
character*(*) line !this is the character line to where we read
...
...
read (unit=11, '(A80)') line
所以在文件(*.txt-file)中,这一行是:
R=
该行的最后一个字符是空白字符(空格)。
有没有办法找出那一行是 3 个字符长,最后一个字符是空白的?
谢谢!
有没有办法找出那一行是 3 个字符长,最后一个字符是空白的?
我只是得到字符数组:'R=',最后有很多空白字符。
答:
0赞
lastchance
9/12/2023
#1
好吧,您几乎可以在 read 语句中使用说明符,但它是可怕的人为。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
评论
0赞
Vladimir F Героям слава
9/12/2023
它在 Fortran 95 中可用吗?老实说,我不知道这些细节。该示例可能会被更改,以证明它实际上并不严格依赖于那里使用的延迟长度字符。
0赞
lastchance
9/12/2023
我认为 Fortran 2003 引入了延迟长度字符,但我不确定 @Mikko T 是否真的被 Fortran 95 卡住了。
0赞
Ian Bush
9/12/2023
是的,延迟长度和分配分配是 F2003,我不确定读取时的大小说明符是什么时候出现的。但和你一样,我不相信 Mikko 仅限于古老的 Fortran95
评论