Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Error 112 /undef when running fortran code

  1. Feb 5, 2013 #1
    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?
     
  2. jcsd
  3. Feb 5, 2013 #2

    SteamKing

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    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.
     
  4. Feb 5, 2013 #3
    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.
     
  5. Feb 5, 2013 #4
    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.
     
  6. Feb 8, 2013 #5
    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
     
  7. Feb 8, 2013 #6
    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?
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Error 112 /undef when running fortran code
  1. Running a Fortran code (Replies: 4)

Loading...