Silverfrost Forums

Welcome to our forums

4 byte or 8 bytes confusion

15 May 2012 2:08 #10157

I'm reading a binary file and in the binary file I have integer and real arrays which are supposed to be 8 byte integers as in the documentation concerning the binary file.

So, I have the follwoing code;

integer*8 geom_data(512) !Declaration of array to be read from the binary file

open(unit=1,file=my_file_name,status='old',form='unformatted',access='direct',recl=512*8)

read(1,rec=record_no)geom_data

.......
     

This works ok.

But now after testing several test cases, I'm encountering a couple of test cases where the binary file has the integer and real arrays of 4 bytes.

So, the above code reads correctly if I have:

integer*4 geom_data(512) !Declaration of array to be read from the binary file

open(unit=1,file=my_file_name,status='old',form='unformatted',access='direct',recl=512*4 

That is, I declare geom_data as 4 bytes. Can anyone help/advise such that the reading is done correctly whether the integer happens to be 8 or 4 bytes in the binary file?

Please help..

Christy

16 May 2012 3:01 #10166

Can anyone please help?

16 May 2012 5:09 #10167

Christy,

does all the files have the same amount of arrays? If yes, you can see if they are 4-bytes or 8-bytes from the file size (use file_size@). If not, you can at least see that they are 4-bytes in the case that the file size is not a manifold of 8*512 (in other words, you have an odd amount of 4-bytes arrays).

Is it possible to read a file first as 4-bytes and then check whether the entries are OK? Then, if not, close the file and open it again as 8-bytes.

Hope this helps a little...

Regards - Wilfried

16 May 2012 8:02 #10168

Wilfried,

Thank you for the response..

  1. If I read as 8 bytes first-it reads some junk/incorrect values. But I do not have any means to verify in the code that the value is a junk.

  2. I did not get about file size.Can you give an example?

Christy

16 May 2012 9:33 #10169

Christy,

(1) ... and if you first read 4 bytes, can you then see if the values seem to be OK?

(2) Example: call file_size@(my_file,fsize,err_code) with characternn my_file, integer4 fsize and integer*2 err_code.

Regards - Wilfried

21 May 2012 8:10 #10201

Hi wilfred, Thanks a lot- I was unwell for 3 days.

1)Thanks for that. Yes, if I read 4 bytes first and the intgers and reals are 8 bytes- I do get an error.

Can you tell me how should the statement be to detect the error (currently the program crashes)? Shall be grateful if helped.

Regards,Christy

21 May 2012 11:12 #10203

Christy,

it might be possible in a way like this:

open(  ... as 4 bytes ...  )
read(...)

! test if the values are OK.
! If not, or if the read command fails:

close(...)
open(  ... as 8 bytes ...  )
read(...)

Hope that helps. Regards - Wilfried

21 Jun 2012 6:13 #10377

Thanks Wilfred..yes- but how to test that a read command fails?

21 Jun 2012 7:14 #10378

Christy,

the Fortran READ can include an error check. I take the example from your first posting here:

open(unit=1,file=my_file_name,status='old',form='unformatted',access='direct',recl=512*4,err=4711)
...
...
4711 print*,'error reading input file'

The entry 'err=4711' means that in case of an error the programme jumps to label 4711.

If you don't get this situation, the programm will give you 512 values à 4 bytes per line. Then check whether these values seems to be OK.

Regards - Wilfried

Please login to reply.