On Jul 23, 3:12=A0pm, Rich Townsend <r...@[EMAIL PROTECTED]
> wrote:
> GaryScott wrote:
> > On Jul 23, 8:44 am, badger <mdeka...@[EMAIL PROTECTED]
> wrote:
> >> On 23 Jul, 08:08, m...@[EMAIL PROTECTED]
wrote:
>
> >>> In a previous article, badger <mdeka...@[EMAIL PROTECTED]
> wrote:
> >>>> Hi,
> >>>> Im new to fortran and Im trying and (failing) to solve my little
> >>>> problem. Any help would be much appreciated?!
> >>>> Im trying to read a text header that I have appended to the top of
a
> >>>> binary file and then read the binary file
> >>>> ie.
> >>>> 1
> >>>> 360
> >>>> 720
> >>>> 0
> >>>> Blah blah blah
> >>>> where blah is the start of the binary file.
> >>>> I can't seem to work out how to read the binary part - i.e. skip
ove=
r
> >>>> the header bit. So far I have
> >>>> PROGRAM read_binary
> >>>> =A0IMPLICIT NONE
> >>>> =A0 =A0INTEGER :: num_rows , num_cols , num_frames , sizeofbyte =3D
=
1
> >>>> =A0 =A0INTEGER, ALLOCATABLE, DIMENSION (:) :: pft
> >>>> =A0 =A0INTEGER :: junk(10)
> >>>> =A0 =A0INTEGER :: i, stdin =3D 10, hdr_length =3D 4
> >>>> =A0 =A0INTEGER :: ioerr =3D 0
> >>>> =A0 =A0OPEN ( stdin, FILE =3D '/dev/stdin', STATUS =3D 'old',
ACCESS=
=3D 'stream',
> >>>> &
> >>>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0FORM =3D =A0'FORMATTED',
IOST=
AT =3D ioerr )
> >>>> =A0 =A0IF ( ioerr /=3D 0 ) THEN
> >>>> =A0 =A0 =A0 =A0 =A0 =A0PRINT *, "Error opening file"
> >>>> =A0 =A0 =A0 =A0 =A0 =A0STOP
> >>>> =A0 =A0END IF
> >>>> =A0 =A0DO i =3D 1, hdr_length
> >>>> =A0 =A0 =A0 =A0 =A0 =A0READ ( stdin, *, iostat =3D ioerr )
hdr_stuff=
(i)
> >>>> =A0 =A0 =A0 =A0 =A0 =A0IF ( ioerr /=3D 0 ) THEN
> >>>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0PRINT *, "Error reading file
=
header"
> >>>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0STOP
> >>>> =A0 =A0 =A0 =A0 =A0 =A0END IF
> >>>> =A0 =A0END DO
> >>>> =A0 =A0num_frames =3D hdr_stuff(1)
> >>>> =A0 =A0num_rows =A0 =3D hdr_stuff(2)
> >>>> =A0 =A0num_cols =A0 =3D hdr_stuff(3)
> >>>> =A0 =A0ALLOCATE ( pft(num_rows * num_cols * num_frames) )
> >>>> =A0 =A0PRINT *, num_frames
> >>>> =A0 =A0PRINT *, num_rows
> >>>> =A0 =A0PRINT *, num_cols
> >>>> =A0 =A0CLOSE ( stdin, IOSTAT =3D ioerr )
> >>>> =A0 =A0IF ( ioerr /=3D 0 ) THEN
> >>>> =A0 =A0 =A0 =A0 =A0 =A0PRINT *, "Error closing file"
> >>>> =A0 =A0 =A0 =A0 =A0 =A0STOP
> >>>> =A0 =A0END IF
> >>>> =A0 =A0OPEN ( stdin, file =3D '/dev/stdin', form =3D 'unformatted',
=
access =3D
> >>>> 'direct', &
> >>>> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 recl =3D
=A0=
sizeofbyte, iostat =3D ioerr )
> >>>> =A0 =A0IF ( ioerr /=3D 0 ) THEN
> >>>> =A0 =A0 =A0 =A0 =A0 =A0PRINT *, "Error opening file for bin"
> >>>> =A0 =A0 =A0 =A0 =A0 =A0STOP
> >>>> =A0 =A0END IF
> >>>> END PROGRAM read_binary
> >>>> As I said I think that the way I have approached it thus far is ok
-
> >>>> open file read header, close file, reopen file and try and read
> >>>> binary. Only I have no idea how to read the binary part - I can't
se=
em
> >>>> to skip over the binary bit!!
> >>>> Thanks.
> >>> =A0 Depends what binary means - sometimes that will have record
sizes=
..
> >>> Anyway - assuming it is "straight binary" - I.e. you can
> >>> read in any amount anytime and not lose anything,
> >>> I would suggest reading the header unformatted- as a
> >>> array of bytes - which could be equivalenced to a string,
> >>> on wbich you could do an internal read.
> >>> Chris
> >> Thanks - but...
> >> As I said and showed in my code - I can read the header bit fine. I
> >> then close the file, and reopen it to read the binary (bytes in this
> >> case) but I have no idea how to step across the header again when I
> >> reopen it and try to read the file as binary?- Hide quoted text -
>
> >> - Show quoted text -
>
> > One possible hint: =A0If the header size is fixed (or you otherwise
kno=
w
> > its size), just read it into a dummy/unused variable and discard it
> > (define a second header (or reuse the old variable if you're done with
> > it or have saved it off) and include it on the "binary" read (assuming
> > it represents the proper size).
>
> Isn't there a big oversight here, that UNFORMATTED is not the same as a
r=
aw data
> stream? On many systems, UNFORMATTED has codes inserted that describe
the=
size
> of the next bit of data. These codes will be gibberish if you try to
read=
a
> piece of text as UNFORMATTED.
Not really, I said it was a hint. If the format of the file is known
and it does contain such extra record pre/suffixes, then the solution
is still simple, just add appropriate sized data item(s) to the binary
read list. You probably only need the first one. I usually like to
leave some intrique.
>
> cheers,
>
> Rich
>
> PS Been away for a while... hi everyone!- Hide quoted text -
>
> - Show quoted text -


|