Re: Q: About reading files into an array without knowing the size. [message #5336 is a reply to message #5205] |
Tue, 07 November 1995 00:00   |
meron
Messages: 51 Registered: July 1995
|
Member |
|
|
In article <309FD207.167E@pfc.mit.edu>, "Thomas W. Fredian" <twf@pfc.mit.edu> writes:
> 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.
> --
Years ago I wrote a routine called read_ascii which reads data from an
ASCII file. The routine finds on the go the number of rows and
columns, as long as the number of entries on each line is the same.
It is possible to have lines of text imbedded in the file, the routine
will ignore them. Finally, it is called as a function so it returns
the data as an array.
Since the routine calls on other routines from my library, it is
advisable to copy the whole library and sort things out later. The
library, in tar form is accessible on cars.uchicago.edu in the pub/idl
directory. The file name is cars_library.tar . You can log in as
anonymous and get it.
Mati Meron | "When you argue with a fool,
meron@cars3.uchicago.edu | chances are he is doing just the same"
|
|
|