vishesh
Nearly a Posting Virtuoso
1,381 posts since Oct 2006
Reputation Points: 85
Solved Threads: 42
Of course it won't. You could see all scraps. Binary files are not the one which are written as 01010101011110. It means that the data is encoded as binary.
vishesh
Nearly a Posting Virtuoso
1,381 posts since Oct 2006
Reputation Points: 85
Solved Threads: 42
So, what should I do? Can you explain a little more.
What do you want to do? Read More .
vishesh
Nearly a Posting Virtuoso
1,381 posts since Oct 2006
Reputation Points: 85
Solved Threads: 42
I wrote one as follows.
string line;
ifstream fileread ("<em>path to the binary file</em>");
while(! fileread.eof())
{
getline(fileread, line);
cout << line << endl;
}
fileread.close();
Well I read a binary file using this, the command prompt is really mad. It,s not displays as a binary code, I mean bunch of zeros and ones.
Can you say where I'm going wrong.
Two problems:
1: Binary data is notstring data. You should probably be using a char array.
2: while(! fileread.eof()) will not work properly. See this ( feof() is the same as .eof() )
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
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.
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.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
>> .. 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),
#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) ;
}
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
Do you need to read in your file as binary, or is it just a plain text file?
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
Because the quick and easy way is usually more costly in the long run? :D
How's that? can you show an example?You don't have to read it one byte at a time if that's what you're worried about.
// Get the binary data
char iobuf[1024];
while ( fileread.read( iobuf, sizeof( iobuf ) ) {
contents += iobuf;
}
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.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 :)
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
>>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.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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.
Dave Sinkula
long time no c
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314