Silverfrost Forums

Welcome to our forums

READ Statement question

19 Jan 2015 6:52 #15347

Hi Group,

The following code is designed to read data from a file:

* FILE: READ_ID.FOR
* SUBROUTINE TO READ FIRST TO RECORD OF A FILE
***********************************************************************
      SUBROUTINE READ_ID(UNIT,NUM,ERROR)
      IMPLICIT NONE
      INTEGER NUM,UNIT
      CHARACTER *60 STRING
      LOGICAL ERROR

      ERROR = .FALSE.
      READ(UNIT,'(A)',ERR=99)STRING
      READ(UNIT,*,ERR=99)NUM

      RETURN
99    PRINT*,'ERROR in reading file header.'
      ERROR = .TRUE.
      RETURN
      END

Can anyone tell me please what is the purpose of '(A)' in the following code line:

READ(UNIT,'(A)',ERR=99)STRING

Thanks for any comments.

19 Jan 2015 7:43 (Edited: 19 Jan 2015 7:50) #15348
READ(UNIT,'(A)',ERR=99)STRING

means the same as

READ(UNIT,10,ERR=99)STRING
10 FORMAT(A)

The format specifier A means read a string of 60 characters then advance to the next record. It is 60 because the variable STRING is of length 60.

If you just want to skip read a record, as in your code STRING isn't actually used, you can use an empty format specifier and no data items, like this:

READ(UNIT,'()',ERR=99)
19 Jan 2015 7:50 #15349

You can read string variables using a list directed input statement, instead of using the A format specifier. However, if the input line contains one or more blanks, the string read ends at the first blank even if the string variable can hold more. For example, if the input line is 'Setting Environment' and the character variable has size 30, reading with list-directed I/O gives 'Setting' whereas the A format gives the entire input string.

27 Jan 2015 3:13 #15483

Thank you for the clarification.

Please login to reply.