Error 112 /undef when running fortran code

Click For Summary

Discussion Overview

The discussion revolves around a Fortran programming issue related to an "Error 112, Reference to undefined variable, array elements or function result" that occurs when attempting to read from a file. Participants explore potential causes of the error, focusing on file naming and variable definitions.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant notes that the OPEN statement may have a syntax issue due to a missing quote in the FILE option.
  • Another participant suggests that the definition and initialization of the variable "number" could be leading to unexpected file names, potentially causing the read operation to fail.
  • A later reply proposes that the file name might contain blank spaces, which could be problematic, and recommends string manipulation to create a cleaner file name.
  • Participants express a desire to see the entire content of the file being read to better diagnose the issue.

Areas of Agreement / Disagreement

There is no consensus on the exact cause of the error, with multiple competing views on how the file name and variable definitions may be contributing to the problem.

Contextual Notes

Participants highlight potential issues with the file name format and variable initialization, but do not resolve these concerns or provide definitive solutions.

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?
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 9 ·
Replies
9
Views
7K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
3
Views
2K