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 -