问题
I want to do some arithmetic operation to an array of real number and later I have to read it as an input for character variable. I used read statement still I get the error as
UNIT SPECIFICATION MUST BE AN INTEGER OR CHARACTER VARIABLE.
I also verified the format descriptor. Here is my piece of code
real::la(10), sl
integer::i
character(len=5)::lat
character(len=7)::station
sl=11.25
do i=1,10
la = sl+ (i*0.25)
read(la(i),'(F5.2)')lat
station= lat//'xx'
end do
回答1:
When you have
read(la(i),'(F5.2)') lat
you are asking to read from the unit la(i)
(external file) into the character variable lat
. This isn't what you want, but is also wrong. This wrongness results in the error message you see: the unit number must be an integer.
However, correcting la
to integer is not what you want to do.
Instead, you want to do an internal write to the character variable lat
:
write(lat, '(F5.2)') la(i)
回答2:
The F5.2
format specifies that a real
value is being read. lat is a a string of characters. The two are not compatible.
The simplest fix is to read to a variable that is real
.
Alternative, change the format to A
(indicating a string), then do an internal read from lat
using the F5.2
format.
来源:https://stackoverflow.com/questions/29489920/how-to-read-real-numbers-as-characters