Re: Q: About reading files into an array without knowing the size. [message #5341 is a reply to message #5205] |
Tue, 07 November 1995 00:00   |
Thomas W. Fredian
Messages: 1 Registered: November 1995
|
Junior Member |
|
|
David van Kuijk wrote:
>
> Hi
>
> One of the nice things of IDL is that it is possible to read whole
> ASCII-files of data (e.g. floats) into an array in one swoop, without
> having to go through a while loop which reads all of these numbers one by
> one. E.g.:
>
> OPENR,1, "filename"
> floatss=FLTARR(10000)
> READF, 1, floatss
>
...
I just tried to write a simple procedure with an on_ioerror to trap the
end of file error and it seems to work fairly well:
function readarray,unit
f = fstat(unit)
maxvals=f.size
answer=fltarr(maxvals) ; Allocate an array guaranteed to be big enough
on_ioerror,finish ; Trap end of file error
readf,unit,answer ; Read in what values are there
finish:
f = fstat(unit)
return,answer(0:f.transfer_count-1) ; return the truncated array
end
I tested this with IDL Version 4.0.1(vms alpha) and it seems to work.
--
/*************************************/
Thomas W. Fredian
Plasma Fusion Center
Massachusetts Institute of Technology
email: twf@pfc.mit.edu
/*************************************/
|
|
|