Re: read binary file question

From: Arjen Markus (arjen.markus_at_wldelft.nl)
Date: 03/24/05


Date: Thu, 24 Mar 2005 08:51:26 +0100

glen herrmannsfeldt wrote:
>
> sarah wrote:
>
> > I have a question of reading binary file. I noticed that using two
> > "read" instead of put it into one "read":
> > read(60) (Tyspec(j,i,n,mz),i=1,nspc+3),
> > 1 (Tyrate(j,i,n,mz), i=1, nspc+1),
> > 1 (((Atrans(mm,nn,kk,j,n,mz),mm=1,2),nn=1,2)
> > 1 ,kk=1,2)!,(TyJac(i,j,n,mz),i=1,(nspc+1)*2)
> > read(60) (TyJac(i,j,n,mz),i=1,(nspc+1)*2)
> > will cause error message which says that "end-of-file during read". I
> > kind of understand it's because the "read" command will read some
> > header first, then there's not enought data to read.
> >
> > For some reason, I do need to read those datas by using seperate
> > line(with some calculation in between). Is there a way to solve this?
>
> The READ must match the WRITE. (You are allowed to read less, and the
> rest will be ignored, but not more.)
>
> If you need to read it in two, then you should write it in two.
>
> Well, there is one way. You can do the first read and read only
> part of the data, do your calculations, BACKSPACE, and then read the
> entire record, including the part you read before. You can use
> different variables if that helps, and then ignore them.
>
> If you have a chance to change the program that is writing, that is
> a better solution.
>
> -- glen

Note that the Fortran standard does not define "binary" files, but
rather "unformatted" files.

There is a very distinct difference that you are encountering right
now:
- unformatted files are files with a record structure - each read
  and write involves a single record. As Glen explained, they
  should match each other:
  - the write statement defines the length of the record
  - the read statement must not read more data than are
    available in that record
  - they both move on to the start of the next record.
- binary files, as commonly used in C (known as streams
  in Fortran 2003, IIRC), have no record structure at all.
  Some compilers offer an extension, often invoked by
  FORM='BINARY' in the open statement. This means the read
  and write statements have no clue as to how long a record
  is or should be. The position of the next read/write in the
  file is defined by how many data were read/written.

Regards,

Arjen



Relevant Pages

  • Re: Debug Assertion Failure
    ... I would read/write structure bytes to file *only* if it is a plain ... You're clearing memory, you must be sure that you are not touching ... Is 'offset' consistent with your binary file format? ...
    (microsoft.public.vc.mfc)
  • Re: Data Structures
    ... Andrew Peskin wrote: ... can you read/write to a binary file, whole objects, in ... > your example above, can you write $foo to a binary file, then read the ...
    (comp.lang.php)
  • Binary data representation
    ... function will read/write from a binary file. ... My question is is there some sort on defined standart to use when ...
    (comp.lang.cpp)