Hi,
The best scheme would be to have the read routine simply return the
number of <processor defined units> (e.g. bytes) read prior to any file
markers being reached (hoping that stream I/O supports this in F2k).
This would allow you to simply buffer (say 1k or 32k or whatever size
buffers) "any" size file in without having to resort to OS-specific
calls to determine total file size, attempting to allocate 2GB (or
whatever size required) of space to read the entire file in at once, or
reading the file one byte at a time to count the number of bytes and
then allocating a buffer of that size (again which may be multiple GB).
Not that there aren't situations (e.g. very small files) where the above
might be a preferred method.
David Vowles wrote:
>
> The short answer is that there is no direct Fortran equivalent.
>
> But there is nothing to stop you writing your own code to emulate
> Matlabs behaviour.
>
> A simple approach is to read the file twice. On the first pass count the
> number of elements. Then allocate storage to hold the data. Then, on the
> second pass read the data into the array(s).
>
> Alternatively, you could use a one-pass reading scheme. Preallocate
> storage. Read the data and reallocate if your preallocated storage is
> inadequate.
>
> And there are probably many other alternatives.
>
> Behind the scenes I suspect Matlab implements a dynamic (re)allocation
> scheme.
>
> David.
>
> Zhou Yong Cheng wrote:
> >
> > It's maybe a simple question. Suppose you have several data files, each
> > of them have different length and will be read in by your program. Usually
> > you have to specify the corresponding array length in your program
> > otherwise you would run in trouble in read. I want to know if there is a
> > mean in Fortran to read data file without specify the size, something like
> > that in MATLAB:
> >
> > r1=myfile(:,1)
> >
> > or
> >
> > fscanf(fid,'%g %g',[2 inf]);
> >
> > Thus, all the data will be read in and we can get the size of array by
> > simple command size(...).
> >
> > Many thanks.
> >
> > Yongcheng
--
Gary Scott
mailto:[log in to unmask]
mailto:[log in to unmask]
http://www.fortranlib.com
Support the GNU Fortran G95 Project: http://g95.sourceforge.net
|