forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

READ Statement question

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support
View previous topic :: View next topic  
Author Message
mike.smartcam



Joined: 02 Aug 2014
Posts: 40
Location: Lancashire, United Kingdom

PostPosted: Mon Jan 19, 2015 7:52 pm    Post subject: READ Statement question Reply with quote

Hi Group,

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

Code:
* 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:

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


Thanks for any comments.
_________________
Regards

Mike
Back to top
View user's profile Send private message Send e-mail
davidb



Joined: 17 Jul 2009
Posts: 560
Location: UK

PostPosted: Mon Jan 19, 2015 8:43 pm    Post subject: Reply with quote

Code:

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


means the same as
Code:

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:

Code:

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

_________________
Programmer in: Fortran 77/95/2003/2008, C, C++ (& OpenMP), java, Python, Perl


Last edited by davidb on Mon Jan 19, 2015 8:50 pm; edited 1 time in total
Back to top
View user's profile Send private message
mecej4



Joined: 31 Oct 2006
Posts: 1886

PostPosted: Mon Jan 19, 2015 8:50 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
mike.smartcam



Joined: 02 Aug 2014
Posts: 40
Location: Lancashire, United Kingdom

PostPosted: Tue Jan 27, 2015 4:13 pm    Post subject: Reply with quote

Thank you for the clarification.
_________________
Regards

Mike
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> Support All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group