Fortran 95, How to read blank (letter space) character from the end of the line?

108 Views Asked by At

I try to modify my program to read lines that contain blank charaters (written with space) at the end of the line. I'm using Intel compiler (Fortran 95).

In the program i'm using the command line:

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

...

...

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

So in the file (*.txt-file), the line is:

R=

The last character of that line is blank character (space).

Is there any way to find out that line is 3 characters long and the last character is blank?

Thanks!

Is there any way to find out that line is 3 characters long and the last character is blank?

I just get character-array : 'R=' with many blank characters in the end.

1

There are 1 best solutions below

3
lastchance On

Well, you could just about use the size= specifier in a read statement, but it's horribly contrived.

The following code allocates (and then reallocates) an allocatable character called 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

For a data file test.dat containing lines

one 
two  

(with the requisite numbers of blanks at the end of each line) it produces, with the square braces [ and ] just to indicate the size of line,

 [one ]           4
 [two  ]           5