Re: reading files

From: Klaus Wacker (wacker_at_physik.uni-dortmund.de)
Date: 07/29/04


Date: 29 Jul 2004 07:30:04 GMT

rand <joleg@nomail.it> wrote:
> I want to read a matrix in binary form like this:
> 122334
> 122333
> 233233
> 334455
>
> My purpose is to read the matrix 3 character per times (also 8 or other) and
> I do this:
> character(3), allocatable :: matrix(:)
> ....
> numbytes=3
> irec=0
> open(11,file=filename,status='unknown',form='unformatted',access='direct',re
> cl=numbytes)
> do i=1,numlines
> irec=irec+1
> read(11,rec=irec) matrix(i)
> enddo
>

Your file looks pretty much like a normal text file consisting of
lines followed by line end characters. Direct access is not
particularly suitable for dealing with this kind files. If all you
want to do is to read the file sequentially, which you do in your
example, just treat it as a formatted sequential file.

I also don't know what you mean by binary. Your file is most certainly
binary in the sense that everything on your computer is. But the
numbers look like decimal numbers to me, which are stored as
characters in the file. So I would do something like:

open(11,file=filename,status='unknown',form='formatted')
do i=1,numlines,2
read(11,'(2A3)') matrix(i),matrix(i+1)
enddo

I leave it as an exercise to deal with odd numlines :-)

-- 
Klaus Wacker              wacker@Physik.Uni-Dortmund.DE
Experimentelle Physik V   http://www.physik.uni-dortmund.de/~wacker
Universitaet Dortmund     Tel.: +49 231 755 3587
D-44221 Dortmund          Fax:  +49 231 755 4547