View on GitHub

IRAF Community Distribution

IRAF maintained by the community

Home | Installation | Packages | X11IRAF | PyRAF | Forum

fscanf

douglas brenner wrote on Nov 07, 2006

Code:

fscanf ("tmp.txt","%f %i", bin, cts)
if (cts > max){
max = cts
fbin = bin
}
}

File:
30621. 14
90599. 2
150577. 3

error: attempt to access undefined local variable bin
but bin is defined as real. What gives?

Francisco Valdes wrote on Nov 07, 2006

Hello,

In the IRAF scripting language if a scan fails then no value is set for the variable. If the variable has no previous value and you try and use it, then you get the error. So you do need to declare the variable but to avoid any error of this type you should set a default value. For instance

real bin
bin = 0.
i = fscanf("temp.text", "%f %i", bin, cnt)
if (bin == 0.)
print ("Oops")


You could also check the value of i which would be how many variables were correctly scanned.

Note that besides this error you also have a misconception of the scan routine. The first argument needs to be a list structured parameter whose value is set to the name of the file.


struct *fd

fd = "temp.text"
while (fscanf (fd, "%f %i", bin, cnt) != EOF) {
...
}
fd = ""


Normally I use "fscan" and let the type of the variables define how the scan is done. You should consult the script programming guide from the documents section.

Yours,
Frank Valdes

Last post on Nov 07, 2006