Fortran Error 112 /undef when running fortran code

AI Thread Summary
A new Fortran user encounters an issue where their code stops with an "Error 112, Reference to undefined variable, array elements or function result" message. The problem arises during a read operation from a file named 'diff//number//.txt', which is incorrectly formatted due to a missing quote in the OPEN statement. The user defines "number" as a CHARACTER variable of length 8, initialized with values in a loop. The resulting file name may include unintended spaces, leading to read errors. Suggestions include checking the concatenated file name for spaces and ensuring proper formatting to avoid confusion for the compiler. It is recommended to manipulate the string to eliminate any spaces and ensure the file name is correctly constructed before attempting to read from it.
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?
 
Technology news on Phys.org
Without seeing more of your code, I don't think a positive answer can be given.

However, in your OPEN statement, the FILE= option is missing ' after 'diff// ...

This could lead to some confusion on the part of the compiler.
 
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?
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top