replica nfl jerseysreplica nfl jerseyssoccer jerseyreplica nfl jerseys forums.silverfrost.com :: View topic - 4 byte or 8 bytes confusion
forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

4 byte or 8 bytes confusion

 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General
View previous topic :: View next topic  
Author Message
christyleomin



Joined: 08 Apr 2011
Posts: 155

PostPosted: Tue May 15, 2012 3:08 pm    Post subject: 4 byte or 8 bytes confusion Reply with quote

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
View user's profile Send private message
christyleomin



Joined: 08 Apr 2011
Posts: 155

PostPosted: Wed May 16, 2012 4:01 am    Post subject: Reply with quote

Can anyone please help?
Back to top
View user's profile Send private message
Wilfried Linder



Joined: 14 Nov 2007
Posts: 314
Location: D�sseldorf, Germany

PostPosted: Wed May 16, 2012 6:09 am    Post subject: Reply with quote

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
View user's profile Send private message
christyleomin



Joined: 08 Apr 2011
Posts: 155

PostPosted: Wed May 16, 2012 9:02 am    Post subject: Reply with quote

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
View user's profile Send private message
Wilfried Linder



Joined: 14 Nov 2007
Posts: 314
Location: D�sseldorf, Germany

PostPosted: Wed May 16, 2012 10:33 am    Post subject: Reply with quote

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
View user's profile Send private message
christyleomin



Joined: 08 Apr 2011
Posts: 155

PostPosted: Mon May 21, 2012 9:10 am    Post subject: Reply with quote

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
View user's profile Send private message
Wilfried Linder



Joined: 14 Nov 2007
Posts: 314
Location: D�sseldorf, Germany

PostPosted: Mon May 21, 2012 12:12 pm    Post subject: Reply with quote

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
View user's profile Send private message
christyleomin



Joined: 08 Apr 2011
Posts: 155

PostPosted: Thu Jun 21, 2012 7:13 am    Post subject: To Wilfred Linder Reply with quote

Thanks Wilfred..yes- but how to test that a read command fails?
Back to top
View user's profile Send private message
Wilfried Linder



Joined: 14 Nov 2007
Posts: 314
Location: D�sseldorf, Germany

PostPosted: Thu Jun 21, 2012 8:14 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group