Re: Reading in to array elements from a file. [message #12273 is a reply to message #12272] |
Wed, 22 July 1998 00:00  |
Kevin Ivory
Messages: 71 Registered: January 1997
|
Member |
|
|
[original message cited below]
Hi Neil,
There are two reasons the readf doesn't work as expected:
1. You cannot read into an array element. The only way to put a value
into a single array element is to read it and assign it in the next
statement:
value = 0
for i = 0, 5 do begin
readf, inlun, value
array[i] = value
endfor
2. Each readf statement reads a new line, so if there is more than one
value per line, have to get all values from that line at once.
Instead of using the for loop, you should get the complete array in
one read. This way, you don't have to worry about the items mentioned
above.
readf, inlun, array
Hope this helps,
Kevin
--
Kevin Ivory Tel: +49 5556 979 434
Max-Planck-Institut fuer Aeronomie Fax: +49 5556 979 240
Max-Planck-Str. 2 mailto:Kevin.Ivory@linmpi.mpg.de
D-37191 Katlenburg-Lindau, GERMANY http://www.gwdg.de/~kivory2/
Neil Rogers wrote:
>
> I wonder if anyone could solve the following mystery for me.
> I am trying to read in *individual* array elements from a file using
> the readf procedure. I have an input file called test.txt containing
> (say)
> 1 2 3 4 5 6
> and the following IDL procedure to read them in and print them out
> again.
> ;------------------
> pro test
> array=intarr(6)
> infile='test.txt'
> openr,inlun,infile,/get_lun
> for cnt=0,5 do begin
> readf,inlun,array[cnt]
> endfor
> close,inlun
> free_lun,inlun
> print,array
> return
> end
> ;-----------------
> Unfortunately, although no compile or run-time errors are encountered,
> nothing is assigned to the array elements and the file pointer stays
> at the beginning of the file. The output is then,
> 0 0 0 0 0 0
> Why is this?
> Thanks for your help.
>
> Neil.
--
Kevin Ivory Tel: +49 5556 979 434
Max-Planck-Institut fuer Aeronomie Fax: +49 5556 979 240
Max-Planck-Str. 2 mailto:Kevin.Ivory@linmpi.mpg.de
D-37191 Katlenburg-Lindau, GERMANY http://www.gwdg.de/~kivory2/
|
|
|