Jeongu Kim
Jeongu Kim

Reputation: 129

How can I know the length of user input with trailing whitespaces in Fortran?

I want to know the length of one line of the user input in Fortran. Since that input can contain some trailing whitespaces, LEN_TRIM does not give the correct answer.

program test

implicit none
character(len=100) :: s

read(*, '(a)') s
write(*, '(i0)') len_trim(s)

end program test

example inputs and outputs:

input: Hello World!

output: 12

expected output: 15

input: (5 spaces)

output: 0

expected output: 5

Upvotes: 2

Views: 665

Answers (3)

Serge3leo
Serge3leo

Reputation: 549

There are several ways:

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

Output:

      25 "Für eigenständig Roß     "
      25 "Für eigenständig Roß     "
      28 "Für eigenständig Roß        "

Upvotes: 1

evets
evets

Reputation: 1026

Read the input one character at a time and count the number read from 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 and LEN_TRIM will still get rid of trailing whitespace. You can test that s(i:i) == ' ' and then set it to some printable character.

Upvotes: 4

So if I understand your question, you would like to include any trailing spaces the user included in his/her input into your query.

That is simply impossible in Fortran formatted I/O from standard input (from keyboard) - I was WRONG see @evets' answer - it is only true (I think now) for advancing output of complete lines. There is no way to distinguish the trailing space included by the user and the blanks normally used too pad all Fortran strings.

Therefore when len_trim() counts the length of the string it cuts away all trailing blanks including those entered by the user. The same holds for the trim() function.

Upvotes: 0

Related Questions