我如何知道 Fortran 中带有尾随空格的用户输入的长度?
How can I know the length of user input with trailing whitespaces in Fortran?
我想知道 Fortran 中用户输入的一行的长度。由于该输入可能包含一些尾随空格,因此 LEN_TRIM 未给出正确答案。
program test
implicit none
character(len=100) :: s
read(*, '(a)') s
write(*, '(i0)') len_trim(s)
end program test
示例输入和输出:
输入:Hello World!
输出:12
预期输出:15
输入:
(5 个空格)
输出:0
预期输出:5
因此,如果我理解您的问题,您希望将 his/her 输入中用户包含的任何尾随 space 包含到您的查询中。
在标准输入(来自键盘)的 Fortran 格式 I/O 中,这根本不可能 - 我错了,请参阅@evets 的回答 - 这只是真的(我think now) 用于推进完整生产线的输出。无法区分用户包含的尾随 space 和通常用于填充所有 Fortran 字符串的空格。
因此,当 len_trim()
计算字符串的长度时,它会删除所有尾随空格,包括用户输入的空格。 trim()
函数也是如此。
一次读取输入的一个字符并计算从STDIN
读取的个数。
program foo
character(len=100) s
integer i
i = 1
do
read(*,'(A1)',eor=10,advance='no') s(i:i)
i = i + 1
end do
10 print *, i - 1 ! Minus for '\n'
end program foo
TRIM
和 LEN_TRIM
仍然会去除尾随空格。
您可以测试 s(i:i) == ' '
然后将其设置为一些
可打印字符。
有几种方法:
program test_getline
use, intrinsic :: iso_fortran_env
implicit none
integer, parameter :: latin1 = selected_char_kind('ISO_10646')
character(*), parameter :: fn = 'test_getline.tmp'
character(30, kind=latin1) s
integer i, ioss, sz, l
open(unit=output_unit, encoding='utf-8')
open(unit=10, file=fn, encoding='utf-8')
write(10, '(a)') 'Für eigenständig Roß '
rewind(10)
! Read line character by character.
l = len(s)
do i = 1, len(s)
read(10, '(a)', iostat=ioss, advance='no') s(i:i)
if(ioss < 0) then
l = i - 1
exit
end if
end do
print *, l, '"', s(:l), '"'
rewind(10)
! Read line without padding, length null terminating string.
s = repeat(char(0, latin1), len(s))
read(10, '(a)', iostat=ioss, pad='no') s
l = index(s, char(0, latin1)) - 1
print *, l, '"', s(:l), '"'
rewind(10)
! Count input character from file.
! For UTF-8 encoding, it can be longer than the string length.
read(10, '(a)', advance='no', iostat=ioss, size=sz) s
print *, sz, '"', s(:sz), '"'
end program test_getline
输出:
25 "Für eigenständig Roß "
25 "Für eigenständig Roß "
28 "Für eigenständig Roß "
我想知道 Fortran 中用户输入的一行的长度。由于该输入可能包含一些尾随空格,因此 LEN_TRIM 未给出正确答案。
program test
implicit none
character(len=100) :: s
read(*, '(a)') s
write(*, '(i0)') len_trim(s)
end program test
示例输入和输出:
输入:Hello World!
输出:12
预期输出:15
输入:
(5 个空格)
输出:0
预期输出:5
因此,如果我理解您的问题,您希望将 his/her 输入中用户包含的任何尾随 space 包含到您的查询中。
在标准输入(来自键盘)的 Fortran 格式 I/O 中,这根本不可能 - 我错了,请参阅@evets 的回答 - 这只是真的(我think now) 用于推进完整生产线的输出。无法区分用户包含的尾随 space 和通常用于填充所有 Fortran 字符串的空格。
因此,当 len_trim()
计算字符串的长度时,它会删除所有尾随空格,包括用户输入的空格。 trim()
函数也是如此。
一次读取输入的一个字符并计算从STDIN
读取的个数。
program foo
character(len=100) s
integer i
i = 1
do
read(*,'(A1)',eor=10,advance='no') s(i:i)
i = i + 1
end do
10 print *, i - 1 ! Minus for '\n'
end program foo
TRIM
和 LEN_TRIM
仍然会去除尾随空格。
您可以测试 s(i:i) == ' '
然后将其设置为一些
可打印字符。
有几种方法:
program test_getline
use, intrinsic :: iso_fortran_env
implicit none
integer, parameter :: latin1 = selected_char_kind('ISO_10646')
character(*), parameter :: fn = 'test_getline.tmp'
character(30, kind=latin1) s
integer i, ioss, sz, l
open(unit=output_unit, encoding='utf-8')
open(unit=10, file=fn, encoding='utf-8')
write(10, '(a)') 'Für eigenständig Roß '
rewind(10)
! Read line character by character.
l = len(s)
do i = 1, len(s)
read(10, '(a)', iostat=ioss, advance='no') s(i:i)
if(ioss < 0) then
l = i - 1
exit
end if
end do
print *, l, '"', s(:l), '"'
rewind(10)
! Read line without padding, length null terminating string.
s = repeat(char(0, latin1), len(s))
read(10, '(a)', iostat=ioss, pad='no') s
l = index(s, char(0, latin1)) - 1
print *, l, '"', s(:l), '"'
rewind(10)
! Count input character from file.
! For UTF-8 encoding, it can be longer than the string length.
read(10, '(a)', advance='no', iostat=ioss, size=sz) s
print *, sz, '"', s(:sz), '"'
end program test_getline
输出:
25 "Für eigenständig Roß "
25 "Für eigenständig Roß "
28 "Für eigenständig Roß "