Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29253 Discussions

Internal read: want to read an integer into a real variable

TommyCee
Beginner
1,144 Views

I want to use an internal read as follows:

if (iStnPress /= 99999) read(iStnPress, '(f5.1)') StnPress

Here, iStnPress is declared an integer; StnPress is declared real.

Before the line is executed, iStnPress has been read as '9245'.

My hope was to have StnPress be set to '924.5'

Unfortunately, I get a runtime crash w/ IVF, with the hint that it's taking '9245' to be a unit number.

Any thoughts?  Am I trying to do the impossible?

 

 

0 Kudos
4 Replies
mecej4
Honored Contributor III
1,144 Views

The first argument of READ should be an integer or a character variable/expression. If the former, that argument is the unit number of an external file. If the latter, that argument is regarded as the contents of an internal file. A useful way to think of the state of affairs is to think of the unit number as a defined channel number from which to read via an unnamed buffer, and the internal file as a named buffer that has already been filled in some way, leaving the original unit number unnamed.

You are trying to make up additional interpretations that are not only outside of the Fortran rules, but in disagreement with the rules. When do you expect an integer to be interpreted as a unit number and when should the same integer be interpreted differently? Secondly, remember that an integer is represented internally in 2-s complement binary. How could you ever read a binary integer using an Fw.d format, and what should w and d stand for? They cannot be counts of decimal digits, so should they be counts of binary, octal or hexadecimal digits? What about the sign, since there is no sign bit for integers?

Why not just write:

     if (iStnPress /= 99999) StnPress = 0.1*iStnPress

 

0 Kudos
TommyCee
Beginner
1,144 Views

Or as I said, I'm trying to do the impossible.  In the past, I've had great success with internal reads like this:

read(StringFull(1:12),'(a12)')  String1

which is a partial indexed read of StringFull.  What I  was trying to above was an impossible extension of that, perhaps trying to be too creative.

Your

 if (iStnPress /= 99999) StnPress = 0.1*iStnPress

sounds fine.  Thanks mecej4.

 

 

 

 

0 Kudos
mecej4
Honored Contributor III
1,144 Views

I'm on my Linux system at the moment, and the runtime of IFort 15.0.2 says

forrtl: No such file or directory
forrtl: severe (29): file not found, unit 9245, file /home/users/XYZ/fort.9245

which is explicit as to the attempted read on a unit numbered 9245. Did you not see a similarly clear message on Windows?

Is is only after these messages that the program aborts with a stack trace.

0 Kudos
TommyCee
Beginner
1,144 Views

Yes, mecej4 - I did see something like that.  That is what I meant when I wrote "with the hint that it's taking '9245' to be a unit number."

Thanks again for your help & tutelage on this.  I'm on my way ...

0 Kudos
Reply