Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28744 Discussions

How to read line with a blank character in the end.

Mikko_fortran_coder
1,789 Views

Hello!

 

I try to modify my program to read lines that contain blank charaters (written with space) at the end of the line.

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!

0 Kudos
10 Replies
Steve_Lionel
Honored Contributor III
1,779 Views

LEN_TRIM will tell you the length without trailing blanks. You can then compare the part of the string after that against blank.

Mikko_fortran_coder
1,769 Views

Hello.

 

Thanks for your answer,

however, the problem is that Len_Trim would return value integer=2, since the last character of the line (in the text-file) is blank (or written with space) : 'R= ' .

So the question is, is there a possibility to recognize these blank characters from the end of the line? (so that the return integer value would be integer=3) ?

 

- Mikko

0 Kudos
andrew_4619
Honored Contributor III
1,753 Views

line looks like a subroutine argument so in the caller is has some length (maybe 80 based on the read)

len(line) will give you that but after the read you will find line is fully padded with trailing spaces to the end irrespective of if there was a space or end of line after the equals. You will see a space in both cases! What are you trying to do?

 

0 Kudos
Mikko_fortran_coder
1,685 Views

andrw_4619 wrote:  "You will see a space in both cases! What are you trying to do?"

 

Well I try to explain this situation more precisely. The textfile from where the lines are read could contain line such as:

Rv =

Now there is a blank character in the end of that line (meaning written with space).

 

Basically i need to find a somekind of method to find out whether the line in the text-file is 'Rv = ' or Rv =' . So the blank character in the end of the line is the problem.

 

Thanks,

Mikko

0 Kudos
Steve_Lionel
Honored Contributor III
1,750 Views

I would think that LEN_TRIM(line)+1 would do this, assuming there was room in the variable for at least one blank.  But I agree with @andrew_4619  that you're probably asking the wrong question.

0 Kudos
mfinnis
New Contributor II
1,678 Views

I'm not sure what the standard says about this and the state of the file afterwards but it seems to work ok with ifort

 

 

 

 

 

  block
      integer :: i_char,iostat,n_char

      do  i_char = 1,Min(Len(line),80)
          read(11,'(a1)',advance = 'no',iostat = iostat) line(i_char:i_char)
          if (iostat /= 0) exit
      end do
      if (iostat /= 0) then
          line(i_char:) = ' '
      else
! Oops          read(11,'(/)')
          read(11,'()')
      end if
      n_char = i_char - 1
  end block

 

 

 

 

 

line will be the same as if read as A80 and n_char will be the number of characters read including trailing spaces.

 

 

jimdempseyatthecove
Honored Contributor III
1,678 Views

You can read the file in Stream mode binary and parse the line breaks.

.OR.

Use the C interoperability to call a C/C++ function to read the lines. (Open the file on the C/C++ side too).

Jim Dempsey

 

Mikko_fortran_coder
1,654 Views

I just started to modify the code, so that the file is opened and read in stream mode binary. I will use this method to solve the problem.

 

Thanks,

Mikko

0 Kudos
mecej4
Honored Contributor III
1,605 Views

Here is another approach for you to consider: create a copy of your data file with all trailing blanks removed. Make your program process the copy, and note that the program can be simpler because it can assume that no input lines have trailing blanks.

0 Kudos
Arjen_Markus
Honored Contributor I
1,670 Views

I have code like this:

! read_line_from_file --
!    Read one complete line as a string from a file
! Arguments:
!    lun         LU-number of the file to be read
!    text        Text string to be created/filled
!    eof         Whether end-of-file was reached or not
! Side effects:
!    Text string is properly initialised
!
subroutine read_line_from_file( lun, text, eof )
    integer, intent(in)              :: lun
    character(len=:), allocatable    :: text
    logical, intent(out)             :: eof

    integer, parameter               :: SEGMENT_LENGTH = 40

    character(len=SEGMENT_LENGTH)    :: segment
    integer                          :: reclen
    logical                          :: eor

    eof  = .false.

    text = ''

    !
    ! Read the record in segments
    !
    do
        read( lun, end = 180, eor = 170, fmt = '(a)', &
              size = reclen, advance='no') segment
        !
        ! Not at the end yet, add to the string
        !
        text = text // segment
    enddo

    !
    ! End of record (end of file will give an empty text string)
    !
170 continue
    text = text // segment(1:reclen)
    return

    !
    ! End of file
    !
180 continue
    text = ''
    eof = .true.
    return

    !
    ! Errors are not handled explicitly ...
    !
end subroutine read_line_from_file

The idea is to read chunks up to the end of the line and concatenate everyting. By using the "size = variablename" option you get exactly the size of the chunks that was read.

Reply