Re: Read a header and then a binary file?
  Home FAQ Contact Sign in
comp.lang.fortran only
 
Advanced search
POPULAR GROUPS

more...

 Up
Re: Read a header and then a binary file?         

Group: comp.lang.fortran · Group Profile
Author: GaryScott
Date: Jul 23, 2008 15:14

On Jul 23, 3:12 pm, Rich Townsend barVOIDtol.udel.edu> wrote:
> GaryScott wrote:
>> On Jul 23, 8:44 am, badger gmail.com> wrote:
>>> On 23 Jul, 08:08, m...@skyway.usask.ca wrote:
>
>>>> In a previous article, badger gmail.com> 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 over
>>>>> the header bit. So far I have
>>>>> PROGRAM read_binary
>>>>>  IMPLICIT NONE
>>>>>    INTEGER :: num_rows , num_cols , num_frames , sizeofbyte = 1
>>>>>    INTEGER, ALLOCATABLE, DIMENSION (:) :: pft
>>>>>    INTEGER :: junk(10)
>>>>>    INTEGER :: i, stdin = 10, hdr_length = 4
>>>>>    INTEGER :: ioerr = 0
>>>>>    OPEN ( stdin, FILE = '/dev/stdin', STATUS = 'old', ACCESS = 'stream',
>>>>> &
>>>>>                    FORM =  'FORMATTED', IOSTAT = ioerr )
>>>>>    IF ( ioerr /= 0 ) THEN
>>>>>            PRINT *, "Error opening file"
>>>>>            STOP
>>>>>    END IF
>>>>>    DO i = 1, hdr_length
>>>>>            READ ( stdin, *, iostat = ioerr ) hdr_stuff(i)
>>>>>            IF ( ioerr /= 0 ) THEN
>>>>>                    PRINT *, "Error reading file header"
>>>>>                    STOP
>>>>>            END IF
>>>>>    END DO
>>>>>    num_frames = hdr_stuff(1)
>>>>>    num_rows   = hdr_stuff(2)
>>>>>    num_cols   = hdr_stuff(3)
>>>>>    ALLOCATE ( pft(num_rows * num_cols * num_frames) )
>>>>>    PRINT *, num_frames
>>>>>    PRINT *, num_rows
>>>>>    PRINT *, num_cols
>>>>>    CLOSE ( stdin, IOSTAT = ioerr )
>>>>>    IF ( ioerr /= 0 ) THEN
>>>>>            PRINT *, "Error closing file"
>>>>>            STOP
>>>>>    END IF
>>>>>    OPEN ( stdin, file = '/dev/stdin', form = 'unformatted', access =
>>>>> 'direct', &
>>>>>                             recl =  sizeofbyte, iostat = ioerr )
>>>>>    IF ( ioerr /= 0 ) THEN
>>>>>            PRINT *, "Error opening file for bin"
>>>>>            STOP
>>>>>    END 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 seem
>>>>> to skip over the binary bit!!
>>>>> Thanks.
>>>>   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:  If the header size is fixed (or you otherwise know
>> 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 raw 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 -
no comments
diggit! del.icio.us! reddit!