View previous topic :: View next topic |
Author |
Message |
christyleomin
Joined: 08 Apr 2011 Posts: 155
|
Posted: Tue May 15, 2012 3:08 pm Post subject: 4 byte or 8 bytes confusion |
|
|
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;
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:
Code: | 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 |
|
Back to top |
|
 |
christyleomin
Joined: 08 Apr 2011 Posts: 155
|
Posted: Wed May 16, 2012 4:01 am Post subject: |
|
|
Can anyone please help? |
|
Back to top |
|
 |
Wilfried Linder
Joined: 14 Nov 2007 Posts: 314 Location: D�sseldorf, Germany
|
Posted: Wed May 16, 2012 6:09 am Post subject: |
|
|
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 |
|
Back to top |
|
 |
christyleomin
Joined: 08 Apr 2011 Posts: 155
|
Posted: Wed May 16, 2012 9:02 am Post subject: |
|
|
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 |
|
Back to top |
|
 |
Wilfried Linder
Joined: 14 Nov 2007 Posts: 314 Location: D�sseldorf, Germany
|
Posted: Wed May 16, 2012 10:33 am Post subject: |
|
|
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 character*nn my_file, integer*4 fsize and integer*2 err_code.
Regards - Wilfried |
|
Back to top |
|
 |
christyleomin
Joined: 08 Apr 2011 Posts: 155
|
Posted: Mon May 21, 2012 9:10 am Post subject: |
|
|
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 |
|
Back to top |
|
 |
Wilfried Linder
Joined: 14 Nov 2007 Posts: 314 Location: D�sseldorf, Germany
|
Posted: Mon May 21, 2012 12:12 pm Post subject: |
|
|
Christy,
it might be possible in a way like this:
Code: | 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 |
|
Back to top |
|
 |
christyleomin
Joined: 08 Apr 2011 Posts: 155
|
Posted: Thu Jun 21, 2012 7:13 am Post subject: To Wilfred Linder |
|
|
Thanks Wilfred..yes- but how to test that a read command fails? |
|
Back to top |
|
 |
Wilfried Linder
Joined: 14 Nov 2007 Posts: 314 Location: D�sseldorf, Germany
|
Posted: Thu Jun 21, 2012 8:14 am Post subject: |
|
|
Christy,
the Fortran READ can include an error check. I take the example from your first posting here:
Code: | 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 |
|
Back to top |
|
 |
|