943,744 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4671
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Sep 5th, 2007
0

Re: Read a binary file

why not just do it the quick and easy way, assuming the binary file is no larger than the char buffer. If the size is unknown than we'd have to first find out the file size and allocate the buffer appropriately. In any case it will be a lot faster then reading the fine one byte at a time.

Also, make sure to open the file in binary mode, not the default text mode.

C++ Syntax (Toggle Plain Text)
  1. unsigned char iobuf[1024];
  2.  
  3. fileread.read(iobuf,sizeof(iobuf));

Aother problem with using std::string like that is that std::string can not be typecast into a structure like character buffers can. The contents of the binary file may well be a structure and reading it into a std::string would destroy that.
Last edited by Ancient Dragon; Sep 5th, 2007 at 1:32 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Sep 5th, 2007
0

Re: Read a binary file

>> .. really mad. It,s not displays as a binary code, I mean bunch of zeros and ones.

if all you want to do is display the contents of a file as a sequence of bits (i presume that is what you mean by bunch of zeros and ones),
C++ Syntax (Toggle Plain Text)
  1. #include <fstream>
  2. #include <limits>
  3. #include <bitset>
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. ifstream file( __FILE__ ) ; file >> noskipws ;
  10. typedef bitset< numeric_limits<unsigned char>::digits > bitset ;
  11. unsigned char byte ;
  12. while( file >> byte ) cout << bitset(byte) ;
  13. }
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Sep 5th, 2007
0

Re: Read a binary file

Quote ...
why not just do it the quick and easy way
Because the quick and easy way is usually more costly in the long run?

Quote ...
In any case it will be a lot faster then reading the fine one byte at a time.
You don't have to read it one byte at a time if that's what you're worried about.
C++ Syntax (Toggle Plain Text)
  1. // Get the binary data
  2. char iobuf[1024];
  3.  
  4. while ( fileread.read( iobuf, sizeof( iobuf ) ) {
  5. contents += iobuf;
  6. }
Now you don't have to worry about buffer sizes, or find the size of the file, or worry about allocating and freeing memory.

Quote ...
Aother problem with using std::string like that is that std::string can not be typecast into a structure like character buffers can. The contents of the binary file may well be a structure and reading it into a std::string would destroy that.
Why can't you do it? A string has the data() method, and I don't understand why (somestruct)iobuf is any different from (somestruct)contents.data() if they both have the same characters.
Reputation Points: 180
Solved Threads: 34
Posting Whiz
Hamrick is offline Offline
322 posts
since Jun 2007
Sep 5th, 2007
0

Re: Read a binary file

Do you need to read in your file as binary, or is it just a plain text file?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Sep 5th, 2007
0

Re: Read a binary file

Click to Expand / Collapse  Quote originally posted by Hamrick ...
Because the quick and easy way is usually more costly in the long run?
How's that? can you show an example?


Click to Expand / Collapse  Quote originally posted by Hamrick ...
You don't have to read it one byte at a time if that's what you're worried about.
C++ Syntax (Toggle Plain Text)
  1. // Get the binary data
  2. char iobuf[1024];
  3.  
  4. while ( fileread.read( iobuf, sizeof( iobuf ) ) {
  5. contents += iobuf;
  6. }
I don't think std::string class will work with binary data like that. I think the += operator will stop when it encounters the first 0 byte in the iobuf.



Click to Expand / Collapse  Quote originally posted by Hamrick ...
Why can't you do it? A string has the data() method, and I don't understand why (somestruct)iobuf is any different from (somestruct)contents.data() if they both have the same characters.
I don't see a difference either (1) if it works (see above comment) and (2) except that old farts like me just perfer using char arrays when the problem suits it better
Last edited by Ancient Dragon; Sep 5th, 2007 at 3:18 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Sep 5th, 2007
1

Re: Read a binary file

Quote ...
How's that? can you show an example?
I can't, but my teachers like to say that if you don't do something right the first time, you end up doing it again and again to fix the problems. It seems to me that using an array is bad because you lock yourself into a buffer size, and using allocated memory is bad because you have to work harder to make it work right and keep it working. The C++ string fixes both of those because it grows on its own and you don't have to manage it.

Quote ...
I don't think std::string class will work with binary data like that. I think the += operator will stop when it encounters the first 0 byte in the iobuf.
Yeah, I tried it. But assign and append let you pick a count for the incoming string. This works.
C++ Syntax (Toggle Plain Text)
  1. #include <fstream>
  2. #include <iostream>
  3. #include <sstream>
  4. #include <string>
  5.  
  6. using namespace std;
  7.  
  8. #define SETUP 1
  9.  
  10. int main() {
  11. string contents;
  12.  
  13. #ifdef SETUP
  14. ofstream binaryOut( "binary.dat", ios::binary );
  15.  
  16. contents.assign( "C code.\0C code run.\0Run code run.", 33 );
  17. binaryOut.write( contents.data(), contents.size() );
  18.  
  19. binaryOut.close();
  20. #else
  21. ifstream binaryIn( "binary.dat", ios::binary );
  22.  
  23. // Get the binary data
  24. char iobuf[1024];
  25.  
  26. do {
  27. binaryIn.read( iobuf, sizeof( iobuf ) );
  28. contents.append( iobuf, binaryIn.gcount() );
  29. } while ( binaryIn );
  30.  
  31. binaryIn.close();
  32.  
  33. // Print the characters
  34. for ( int i = 0; i < contents.size(); ++i ) {
  35. if ( !isprint( contents[i] ) ) {
  36. cout<< int( contents[i] );
  37. } else {
  38. cout<< contents[i];
  39. }
  40. }
  41.  
  42. cout<<"\n";
  43. #endif
  44.  
  45. return 0;
  46. }
Quote ...
and (2) except that old farts like me just perfer using char arrays when the problem suits it better
But does the problem really suit char arrays better or are the old farts just doing what they're comfortable with? If the file is always a fixed size that's great, but if it's not you have to make a super huge char array to accommodate the expected sizes and add extra logic in case the size is bigger than the array. If you don't want to wrestle with that, you have to dynamically allocate a char array, and that's just a lot of memory management that you don't need to do. It makes the code longer and harder to keep up.
Last edited by Hamrick; Sep 5th, 2007 at 3:57 pm.
Reputation Points: 180
Solved Threads: 34
Posting Whiz
Hamrick is offline Offline
322 posts
since Jun 2007
Sep 5th, 2007
0

Re: Read a binary file

>>I can't, but my teachers like to say that if you don't do something right the first time, you end up doing it again and again to fix the problems
And your teachers are right. Sofware companies spend probably three times (or more) the money in program maintenance then in original development.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Sep 5th, 2007
0

Re: Read a binary file

One thing that the OP could clarify is whether or not it is necessary to read the whole file into memory, or whether it might simply be simpler and easier to process as a stream.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 6th, 2007
0

Re: Read a binary file

Click to Expand / Collapse  Quote originally posted by Hamrick ...
How does that make a difference? A string just handles the memory for the char array instead of making you do it. I don't think there should be any problem with using a string as long as you don't do any text conversions.
C++ Syntax (Toggle Plain Text)
  1. ifstream fileread ("path to the binary file");
  2. string contents;
  3. char ch;
  4.  
  5. // Get the binary data
  6. while ( fileread.get( ch ) ) {
  7. contents.push_back( ch );
  8. }
  9.  
  10. // Print the binary data as hex
  11. for ( int i = 0; i < contents.size(); ++i ) {
  12. cout<< hex << int( contents[i] ) <<" ";
  13. }
  14.  
  15. fileread.close();

Actually later I want to convert some parts to decimal.
Reputation Points: 32
Solved Threads: 2
Junior Poster
eranga262154 is offline Offline
185 posts
since Sep 2007
Sep 6th, 2007
0

Re: Read a binary file

Click to Expand / Collapse  Quote originally posted by vijayan121 ...
>> .. really mad. It,s not displays as a binary code, I mean bunch of zeros and ones.

if all you want to do is display the contents of a file as a sequence of bits (i presume that is what you mean by bunch of zeros and ones),
C++ Syntax (Toggle Plain Text)
  1. #include <fstream>
  2. #include <limits>
  3. #include <bitset>
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. ifstream file( __FILE__ ) ; file >> noskipws ;
  10. typedef bitset< numeric_limits<unsigned char>::digits > bitset ;
  11. unsigned char byte ;
  12. while( file >> byte ) cout << bitset(byte) ;
  13. }
Great, Thanks.

But I need one thing to do there. I want to count the number of bits. I'll try it now. Do you have any suggestions.
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: passing arrays / updating object arrays
Next Thread in C++ Forum Timeline: using if-else





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


Follow us on Twitter


© 2011 DaniWeb® LLC