943,985 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3613
  • C++ RSS
Sep 18th, 2007
0

Skip initial 4 bytes in a binary stream

Expand Post »
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.
Similar Threads
Reputation Points: 32
Solved Threads: 2
Junior Poster
eranga262154 is offline Offline
185 posts
since Sep 2007
Sep 18th, 2007
0

Re: Skip initial 4 bytes in a binary stream

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Sep 18th, 2007
0

Re: Skip initial 4 bytes in a binary stream

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.

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 32
Solved Threads: 2
Junior Poster
eranga262154 is offline Offline
185 posts
since Sep 2007
Sep 18th, 2007
0

Re: Skip initial 4 bytes in a binary stream

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
C++ Syntax (Toggle Plain Text)
  1. while( fileopen >> c)
  2. {
  3. // blabla
  4. }
Last edited by Ancient Dragon; Sep 18th, 2007 at 9:09 am.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Sep 19th, 2007
0

Re: Skip initial 4 bytes in a binary stream

Thanks, I'll work with your suggestion and if I found any difficulties right back here.
Reputation Points: 32
Solved Threads: 2
Junior Poster
eranga262154 is offline Offline
185 posts
since Sep 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help with code.
Next Thread in C++ Forum Timeline: Conversion from char to decimal(int)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC