Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Fortran > RE: Read a head...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 13 Topic 8543 of 8774
Post > Topic >>

RE: Read a header and then a binary file?

by meek@[EMAIL PROTECTED] Jul 23, 2008 at 07:08 AM

In a previous article, badger <mdekauwe@[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 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
 




 13 Posts in Topic:
Read a header and then a binary file?
badger <mdekauwe@[EMAI  2008-07-23 03:40:49 
RE: Read a header and then a binary file?
meek@[EMAIL PROTECTED]   2008-07-23 07:08:14 
Re: Read a header and then a binary file?
badger <mdekauwe@[EMAI  2008-07-23 06:44:33 
Re: Read a header and then a binary file?
GaryScott <garylscott@  2008-07-23 07:37:33 
RE: Read a header and then a binary file?
meek@[EMAIL PROTECTED]   2008-07-23 09:17:28 
Re: Read a header and then a binary file?
Rich Townsend <rhdt@[E  2008-07-23 15:12:36 
Re: Read a header and then a binary file?
nospam@[EMAIL PROTECTED]   2008-07-23 15:27:51 
Re: Read a header and then a binary file?
Rich Townsend <rhdt@[E  2008-07-23 22:08:42 
Re: Read a header and then a binary file?
nospam@[EMAIL PROTECTED]   2008-07-23 23:23:20 
Re: Read a header and then a binary file?
glen herrmannsfeldt <g  2008-07-24 05:26:38 
Re: Read a header and then a binary file?
glen herrmannsfeldt <g  2008-07-24 11:43:44 
Re: Read a header and then a binary file?
GaryScott <garylscott@  2008-07-23 09:00:23 
Re: Read a header and then a binary file?
GaryScott <garylscott@  2008-07-23 15:14:19 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sun Oct 12 23:43:16 CDT 2008.