Dynamic array

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2005
Posts: 15,477
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: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Read a binary file

 
0
  #11
Sep 5th, 2007
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.

  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.
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: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Read a binary file

 
0
  #12
Sep 5th, 2007
>> .. 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),
  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. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: Read a binary file

 
0
  #13
Sep 5th, 2007
why not just do it the quick and easy way
Because the quick and easy way is usually more costly in the long run?

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.
  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.

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.
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Read a binary file

 
0
  #14
Sep 5th, 2007
Do you need to read in your file as binary, or is it just a plain text file?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,477
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: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Read a binary file

 
0
  #15
Sep 5th, 2007
Originally Posted by Hamrick View Post
Because the quick and easy way is usually more costly in the long run?
How's that? can you show an example?


Originally Posted by Hamrick View Post
You don't have to read it one byte at a time if that's what you're worried about.
  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.



Originally Posted by Hamrick View Post
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.
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: Jun 2007
Posts: 322
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: Read a binary file

 
1
  #16
Sep 5th, 2007
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.

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.
  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. }
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.
The truth does not change according to our ability to stomach it.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,477
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: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Read a binary file

 
0
  #17
Sep 5th, 2007
>>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.
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: Apr 2004
Posts: 4,393
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 244
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Read a binary file

 
0
  #18
Sep 5th, 2007
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.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 185
Reputation: eranga262154 is an unknown quantity at this point 
Solved Threads: 2
eranga262154's Avatar
eranga262154 eranga262154 is offline Offline
Junior Poster

Re: Read a binary file

 
0
  #19
Sep 6th, 2007
Originally Posted by Hamrick View Post
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.
  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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 185
Reputation: eranga262154 is an unknown quantity at this point 
Solved Threads: 2
eranga262154's Avatar
eranga262154 eranga262154 is offline Offline
Junior Poster

Re: Read a binary file

 
0
  #20
Sep 6th, 2007
Originally Posted by vijayan121 View Post
>> .. 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),
  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.
Reply With Quote Quick reply to this message  
Reply

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


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC