Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fortran 77 - read from an init file - add comments 1

Status
Not open for further replies.

Widukin

Programmer
Feb 8, 2003
11
FR
Hi,

I have an input file for a fortran 77 program and since there are many lines and many constants to be given, it has become quite complicated to read.

I would like to add comments on this input file but I don't want him to read them, just jump over anything that is NAN, something like :

initial point: 3.0 0.0
end point: 3.0 0.8
stretch: 5.0E-5
angle: 90

Thanks,

Widukin
 
The routine getval below expects an array. For single variables, I've equivalenced them with an array of dimension 1 to keep the compiler happy.

Code:
      subroutine getval (chan, val, count)
      integer chan
      real val(*)
      integer count
      integer i, start
      character*80 line
      read (chan, '(A)') line
      start = index (line, ':')
      if (start .ne. 0) then
         read (line(start + 1:), *) (val(i), i = 1, count, 1)
      endif
      return
      end
      
      program main
      real initpt(2)
      real endpt(2)
      real stretch, stretchalt(1)
      real angle, anglealt(1)
      equivalence (stretch, stretchalt(1))
      equivalence (angle, anglealt(1))
      integer chan
      data chan /10/
      
      open (chan, file='data.txt', status='unknown')
      call getval (chan, initpt, 2)
      call getval (chan, endpt, 2)
      call getval (chan, stretchalt, 1)
      call getval (chan, anglealt, 1)
      close (chan)
      
      print *, 'init ', initpt(1), initpt(2)
      print *, 'end  ', endpt(1), endpt(2)
      print *, 'stretch ', stretch
      print *, 'angle ', angle
      stop
      end
 
Thanks, it was really usefull and I got to do a comprehensive input file. Since you are at ease with this kind of question I would like to ask another one :). If then I have a file such as:

********* general information *********

date (IDATE): 20/07/2005.
title (ITITRE): fiber

********* Definition of the fiber *********

Number of available space steps (NH): 1
initial point: 3.0 0.0
end point: 3.0 0.8
stretch: 5.0E-5
angle: 90.

What can I add or with what format so that I can jump the empty lines and the *** remarks.

Another question, since I have double floats, strings and integers, should I make 3 different functions to apply to each case?

Thanks again,

Widukin
 
Code:
      subroutine getval (chan, val, count)
      integer chan
      real val(*)
      integer count
      integer i, start
      character*80 line
C     Skip until we find something with a value
10    continue
      read (chan, '(A)', end=20) line
      start = index (line, ':')
      if (start .eq. 0) goto 10
C     Get the value
      read (line(start + 1:), *) (val(i), i = 1, count, 1)
20    return
      end

Yes - you will have to write different subroutines: one for each type
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top