[net.lang.f77] How can I get free-form input to work?

wcs@ho95b.UUCP (01/17/84)

----
	I'm trying to do free-form input in f77 on 4.1BSD, and I
can't get it to work.  The basic structure for the input file that I
want is a collection of lines, each of which describes an operation.
The lines all begin with a format-identifier, but have different
types of arguments after that.  For example:

	'file' 'file-type' 'file-name' Iarg4 Iarg5
	'scale'	'x' Farg3 Farg4 Farg5 Iarg6
	'scale' 'y' Farg3 Farg4 Farg5 Iarg6
	'trace' Iarg2
	'plot'
	'quit'

Unfortunately, each READ statement reads up to a newline, and
discards any unused data on the line.  What I wanted to do was read
the first field, then do one or two more reads on the line, with the
variables and their types dependent on the previous data.  The best
I've been able to do so far is to use list-directed input, with a
separate line for each batch of stuff to be read:
	'file'
	'file-type'
	'filename' Iarg4 Iarg5
	'scale'
	'x' Farg3 Farg4 Farg5 Iarg6 
	....
A typical READ statement is READ(5,*) XORY, LOW, HIGH, INCHES, ISEXAC

I've read that UNIX F77 will treat a file as one long character
stream if you open the file with RECL=1.  Unfortunately, whenever I
put RECL=1 in the open statement, f77 decided to use DIRECT access
instead of SEQUENTIAL, and list-directed i/o only works with
SEQUENTIAL.  Has anyone gotten something like this to work?

			Thanks;
				Bill Stewart BTL-Holmdel NJ
				{.....BTL..}!ho95b!wcs
				ucbvax!ihnp4!ho95b!wcs
				decvax!harpo!ho95b!wcs

hopp@nbs-amrf.UUCP (01/18/84)

|
V
The easiest way to deal with your problem is to use the internal I/O
capabilities of Fortran 77.  (Internal reads are the replacement for
the encode/decode statements found in many fortran 66 implementations.)
The idea is to read the line into an internal character variable and
then read from that variable into the program variables that you
want.  For instance, the following code reads a line and, based on
the first character, reads the next field as either character or
logical data:

	character*80 line, text
	logical dosay
	read (5,1000) line
6000	format(A)
	if (line(1:1) .eq. 'L') then
C	   logical data...
	   read (line(2:), 1000) dosay
1000	   format (L7)
	   write (6,6000) dosay
6000	   format (' Logical data: ',L7)
	else if (line(1:1) .eq. 'A') then
C	   text data...
	   read (line(2:), 1010) text
1010	   format(A)
	   write (6,6010) text
6010	   format (' Text data: ', A)
	else
	   write (6,6020) line(1:1)
6020	   format (' Error on input field specifier: ',A)
	end if
	stop
	end

The syntax for internal I/O is that a character expression is used
instead of a logical unit number, and I/O is directed to the data area
for that character data rather than to an external file.

Ted Hopp
National Bureau of Standards
Metrology A127
Washington, DC 20234
UUCP: ..!umcp-cs!nbs-amrf!hopp