Skip initial 4 bytes in a binary stream

Reply

Join Date: Sep 2007
Posts: 184
Reputation: eranga262154 is an unknown quantity at this point 
Solved Threads: 2
eranga262154's Avatar
eranga262154 eranga262154 is offline Offline
Junior Poster

Skip initial 4 bytes in a binary stream

 
0
  #1
Sep 18th, 2007
I have a binary file, contain set of stream, and I want to find some information there. What I have done up to now is read the file, open it and count the number of streams three. I'm stuck now.

Just consider one stream. I want to skip first 4 bytes and read the next 4 bytes. The second 4 bytes gives the length of the next part and that is the end of one stream. Same process should follows until EOF is found. My question is how to skip that first 4 bytes and point to the next byte.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Skip initial 4 bytes in a binary stream

 
0
  #2
Sep 18th, 2007
Just read 4 bytes and don't store the result ?

Or use a 'seek' function to advance the stream by 4 bytes ?

A bit of code to fill us in on exactly what kind of stream you're talking about might help.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 184
Reputation: eranga262154 is an unknown quantity at this point 
Solved Threads: 2
eranga262154's Avatar
eranga262154 eranga262154 is offline Offline
Junior Poster

Re: Skip initial 4 bytes in a binary stream

 
0
  #3
Sep 18th, 2007
Ok' I'll put mu code here. Actually I have no important on first four bytes. I need to read next four byes. Here what I have tried up to now.

  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std ;
  5.  
  6. int main ()
  7. {
  8. ifstream fileopen ;
  9. char buffer[100 ] ;
  10. char c ;
  11.  
  12. fileopen.open ( "G00046_002_01.srf", ios :: in | ios :: out | ios :: binary ) ;
  13.  
  14. if(fileopen.is_open())
  15. {
  16. fileopen.seekg(0, ios :: beg) ; // Move pointer to the beginning
  17. while(!fileopen.eof())
  18. {
  19. fileopen.read(buffer, 50) ;
  20. fileopen.seekg(4) ;
  21. fileopen >> c ; // Just to check the file read correctly
  22. cout << c << endl ;
  23.  
  24. // Now I want to read the next 4 bytes here
  25.  
  26. }
  27. fileopen.close() ;
  28. }
  29.  
  30. else
  31. {
  32. cout << "Unable to open the file\n" ;
  33. }
  34.  
  35.  
  36. cin.get() ;
  37. return 0 ;
  38. }

What is your suggestion for me.
Help appreciate
Last edited by Ancient Dragon; Sep 18th, 2007 at 9:04 am. Reason: add line numbers to code tags
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,160
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1437
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: Skip initial 4 bytes in a binary stream

 
0
  #4
Sep 18th, 2007
line 16 is not needed because the file pointer is always set to beginning of file when the file is opened -- line 12.

line 17 is an infinite loop because lines 19 and 20 reset the file pointer back to the 4th byte in the file on every loop iteration. Suggestion: delete line 19 altogether because it isn't needed. Move line 20 outside the while loop, to line 16.

don't use eof() function because it doesn't do what you think it does. Code the loop like this
  1. while( fileopen >> c)
  2. {
  3. // blabla
  4. }
Last edited by Ancient Dragon; Sep 18th, 2007 at 9:09 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 184
Reputation: eranga262154 is an unknown quantity at this point 
Solved Threads: 2
eranga262154's Avatar
eranga262154 eranga262154 is offline Offline
Junior Poster

Re: Skip initial 4 bytes in a binary stream

 
0
  #5
Sep 19th, 2007
Thanks, I'll work with your suggestion and if I found any difficulties right back here.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC