| | |
Skip initial 4 bytes in a binary stream
![]() |
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.
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
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.
What is your suggestion for me.
Help appreciate
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> using namespace std ; int main () { ifstream fileopen ; char buffer[100 ] ; char c ; fileopen.open ( "G00046_002_01.srf", ios :: in | ios :: out | ios :: binary ) ; if(fileopen.is_open()) { fileopen.seekg(0, ios :: beg) ; // Move pointer to the beginning while(!fileopen.eof()) { fileopen.read(buffer, 50) ; fileopen.seekg(4) ; fileopen >> c ; // Just to check the file read correctly cout << c << endl ; // Now I want to read the next 4 bytes here } fileopen.close() ; } else { cout << "Unable to open the file\n" ; } cin.get() ; return 0 ; }
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
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
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)
while( fileopen >> c) { // blabla }
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.
![]() |
Similar Threads
- Binary File IO (C#)
- Convert C++ to VB.Net (C++)
- Send data on a serial port (C++)
- Open In New Window Php (PHP)
- c doubts (C++)
- validate-upload file (ASP.NET)
- sizeof() for a Structure (C++)
- I've got Trojan.Holax... is this bad? (Viruses, Spyware and other Nasties)
- not-a-virusadware (Viruses, Spyware and other Nasties)
Other Threads in the C++ Forum
- Previous Thread: Help with code.
- Next Thread: Conversion from char to decimal(int)
| Thread Tools | Search this Thread |
ace_thread api array based binary bitmap borland c++ c/c++ calling char class classes code coding compile console conversion count delete delete-line deploy desktop developer directshow dll download dynamic dynamiccharacterarray email embedded encryption error file forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream information input int integer java lib linkedlist linker loop looping loops map math mathhomeworkhelp matrix memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion reference reverse richedit rpg string strings temperature template test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets






