Curious file call

I have the following few lines of code in a program I am analysing 9still):

read (iunit, '(42x,a)') filez
read (filez, *) start_zones, increment_zones

The interesting thing is that the line it is reading from the file in the first instance is this one…

 Starting model & frequency (free format): -1 10

This makes the variable filez equal to ‘-1 10

Now, the following read line appears to work (the program doesn’t crash or generate an error), so what is happening here? What is the effective value of filez as used by the subsequent read statement?

Read the format '(42x,a)' as: “ignore the first 42 characters, then read a character string until the end of the line”; save the result in filez. The second statement extracts the values -1 and 10 from filez.

It’s that last part I do not get. The read statment is looking for a single value, yet it is passed -1 and 10.

What does the read statement do when receiving 2 values? Does it operate on two ‘ports’?

The Asterisk format specifier * in the second statement tells to just read the input according to the variables types. Since start_zones and increment_zones are probably integers, the content of character string filez is interpreted as two integers.

Yes, of course.

And if I had finished doing what I was doing before posting my question, I might have spotted that myself!

I was writing a few lines to output the variable values to a file, rather than simply the filez value, as I had already done. Sometimes, coding alone is a very lonely exercise that causes you to find challenges where there are none! Hahahaha!

Thank you so much. :slight_smile:

The character variable filez, when used as the first argument in a formatted READ statement, is called an “internal file”. Read here about the concept and the properties of such files. Such internal files exist only when your program is running, and the operating system does not know anything about them, or even consider them as files.

To be precise, the a in the format acquires width of its companion object on the I/O list, that is filez, and reads no more characters than that from the rest of the record. If the record is too short, it is padded with blanks (unless pad='no' us used in the open statement).

1 Like