This is the mail archive of the fortran@gcc.gnu.org mailing list for the GNU Fortran project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: READ ( UNIT = TRIM(String),...


Philippe Schaffnit wrote:
Hi!

I was trying to iron out a wrinkle from a piece of code, when I stumbled
onto something which I find quite surprising, what I hold to be a
'reject valid' from GFortran: the 'reject valid' would belong here, the
other bit probably not, but if a kind soul could nevertheless enlighten
me, I would greatly appreciate it!

Take the following piece of code:

!
      PROGRAM Test
!
      IMPLICIT NONE
!
      INTEGER                 ::   Number, IO_Stat
!      CHARACTER ( LEN = 250 ) ::   String = "Hello world!"
      CHARACTER ( LEN = 250 ) ::   String = "/Hello world!"
!
!      READ ( UNIT = TRIM(String), FMT = *, IOSTAT = IO_Stat ) Number
      READ ( UNIT = String, FMT = *, IOSTAT = IO_Stat ) Number
      WRITE ( UNIT = 6, FMT = * ) Number, IO_Stat
!
      END PROGRAM Test
!

The obvious one first: "GNU Fortran (GCC) 4.3.0 20071026 (experimental)
[trunk revision 129646]" rejects "READ ( UNIT = TRIM(String)," with


READ ( UNIT = TRIM(String), FMT = *, IOSTAT = IO_Stat ) Number
                  1
Error: UNIT specification at (1) must be an INTEGER expression or a
CHARACTER variable

Is this warranted by the standard?, I would expect it to be fine: the
SGI, Lahey and Sun compilers seem happy with it... (for the record, the
Intel one doesn't either)

Now for the really weird part, if I remove the 'TRIM' to get over this,
I would expect an IO error to be raised, but I have yet to find a
compiler which does!

The slash is considered a value separator. The value is read until a separator is found. Right from the standard, 10.8:

A value separator is

(1) A comma optionally preceded by one or more contiguous blanks and optionally followed by one or more contiguous blanks,

(2) A slash optionally preceded by one or more contiguous blanks and optionally followed by one or more contiguous blanks, or

(3) One or more contiguous blanks between two nonblank values or following the last nonblank value, where a nonblank value is a constant, an râc form, or an r form.

If you try to read a second value in that READ statement you will get the expected error.

CHARACTER ( LEN = 250 ) :: String = "/Hello world!"

READ ( UNIT = String, FMT = *, IOSTAT = IO_Stat ) Number, number

Regards,

Jerry


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]