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?
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top