| | |
Dynamic array
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
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.
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.
Also, make sure to open the file in binary mode, not the default text mode.
C++ Syntax (Toggle Plain Text)
unsigned char iobuf[1024]; 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.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
>> .. 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),
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)
#include <fstream> #include <limits> #include <bitset> #include <iostream> using namespace std; int main() { ifstream file( __FILE__ ) ; file >> noskipws ; typedef bitset< numeric_limits<unsigned char>::digits > bitset ; unsigned char byte ; while( file >> byte ) cout << bitset(byte) ; }
•
•
•
•
why not just do it the quick and easy way

•
•
•
•
In any case it will be a lot faster then reading the fine one byte at a time.
C++ Syntax (Toggle Plain Text)
// Get the binary data char iobuf[1024]; while ( fileread.read( iobuf, sizeof( iobuf ) ) { contents += 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.
(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.
•
•
•
•
Because the quick and easy way is usually more costly in the long run?
•
•
•
•
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)
// Get the binary data char iobuf[1024]; while ( fileread.read( iobuf, sizeof( iobuf ) ) { contents += iobuf; }
•
•
•
•
Why can't you do it? A string has the data() method, and I don't understand why(somestruct)iobufis any different from(somestruct)contents.data()if they both have the same characters.
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.
•
•
•
•
How's that? can you show an example?
•
•
•
•
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.

C++ Syntax (Toggle Plain Text)
#include <fstream> #include <iostream> #include <sstream> #include <string> using namespace std; #define SETUP 1 int main() { string contents; #ifdef SETUP ofstream binaryOut( "binary.dat", ios::binary ); contents.assign( "C code.\0C code run.\0Run code run.", 33 ); binaryOut.write( contents.data(), contents.size() ); binaryOut.close(); #else ifstream binaryIn( "binary.dat", ios::binary ); // Get the binary data char iobuf[1024]; do { binaryIn.read( iobuf, sizeof( iobuf ) ); contents.append( iobuf, binaryIn.gcount() ); } while ( binaryIn ); binaryIn.close(); // Print the characters for ( int i = 0; i < contents.size(); ++i ) { if ( !isprint( contents[i] ) ) { cout<< int( contents[i] ); } else { cout<< contents[i]; } } cout<<"\n"; #endif return 0; }
•
•
•
•
and (2) except that old farts like me just perfer using char arrays when the problem suits it better
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.
>>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.
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.
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
•
•
•
•
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)
ifstream fileread ("path to the binary file"); string contents; char ch; // Get the binary data while ( fileread.get( ch ) ) { contents.push_back( ch ); } // Print the binary data as hex for ( int i = 0; i < contents.size(); ++i ) { cout<< hex << int( contents[i] ) <<" "; } fileread.close();
Actually later I want to convert some parts to decimal.
•
•
•
•
>> .. 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)
#include <fstream> #include <limits> #include <bitset> #include <iostream> using namespace std; int main() { ifstream file( __FILE__ ) ; file >> noskipws ; typedef bitset< numeric_limits<unsigned char>::digits > bitset ; unsigned char byte ; while( file >> byte ) cout << bitset(byte) ; }
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.
![]() |
Similar Threads
- get length of a dynamic array (C++)
- Sorting 2D Dynamic array of integers , Snake Style (C)
- works for static need help to make it dynamic (C++)
- dynamic array of structures problem (C++)
Other Threads in the C++ Forum
- Previous Thread: passing arrays / updating object arrays
- Next Thread: using if-else
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






