Error 112 /undef when running fortran code

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
5 replies · 3K views
Marco87
Messages
2
Reaction score
0
Hi,

I am a new Fortran User.

I have a problem when I run my code (not errors during building), in fact the code is stopped in the last line and this message appears "Error 112, Reference to undefined variable, array elements or function result"

...
write(number,100)k1
open (unit=12, file='diff//number//'.txt', status='old', POSITION='APPEND', iostat=ISTATUS, action='read',form='formatted')
READ (12,*, iostat=ISTATUS) i1r, i2r, i3r, sr, kr
close(12)
write(*,*) i1r
...

I checked the iostat value and it is 0 in open and -1 in read.

" 5 10 1 1.00000 1" (This is the first line of my txt file)

Where is the problem?
 
Physics news on Phys.org
The FILE option is BEFORE the "diff", part...see it?

I would like to see how "number" is defined.

I (and you too) would like to see what the resulting file name is when you concatenate diff//number//'.txt' ...it may be that you are getting something line "diff3 .txt" simply because of the way (length) the character variable "number" might have been defined.

I would like to see the entire content of the file you are trying to open and read from.
 
So...put together the file name BEFORE you attempt to use it in the read statement, use another character variable, say, "fname" to put the name into and write out to the terminal to see what it looks like.
 
Hi,

Number is defined in this way:

"CHARACTER(len=8) :: number"

then it is initialized

"write(number,100)1
[...]
do k1=1, k
do
write(number,100)k1

open (unit=12, file='diff'//number//'.txt', status='old', POSITION='APPEND', iostat=ISTATUS, action='read',form='formatted')
READ (12,*, iostat=ISTATUS) i1r, i2r, i3r, sr, kr
close(12)
write(*,*) i1r
[...]
enddo
end do"

in the file "diff 1.txt", i have these number:
5 10 1 1.00000 1
5 10 1 1.00000 1

I can write in this file, when I try to read I have a problem! I think that the line
"READ (12,*, iostat=ISTATUS) i1r, i2r, i3r, sr, kr"
does not work!

Thanks
Marco
 
so you are getting blank spaces as part of the file name? maybe that's your problem...why don't you do some string manipulations or zero-padding on the left or something to get a nice file name without blank spaces in the middle of it?