943,708 Members | Top Members by Rank

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

Read a binary file

Expand Post »
Can someone explain, how can read a binary file. I'm really nervous with this.
Similar Threads
Reputation Points: 32
Solved Threads: 2
Junior Poster
eranga262154 is offline Offline
185 posts
since Sep 2007
Sep 5th, 2007
0

Re: Read a binary file

Google it and you'll get loads of good articles on that:
http://www.google.com/search?ie=UTF-...les+in+c%2B%2B
Reputation Points: 85
Solved Threads: 42
Nearly a Posting Virtuoso
vishesh is offline Offline
1,362 posts
since Oct 2006
Sep 5th, 2007
0

Re: Read a binary file

I wrote one as follows.

string line;
	ifstream fileread ("path to the binary file");

	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.
Last edited by eranga262154; Sep 5th, 2007 at 3:40 am. Reason: Incorrect
Reputation Points: 32
Solved Threads: 2
Junior Poster
eranga262154 is offline Offline
185 posts
since Sep 2007
Sep 5th, 2007
0

Re: Read a binary file

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.
Reputation Points: 85
Solved Threads: 42
Nearly a Posting Virtuoso
vishesh is offline Offline
1,362 posts
since Oct 2006
Sep 5th, 2007
0

Re: Read a binary file

So, what should I do? Can you explain a little more.
Reputation Points: 32
Solved Threads: 2
Junior Poster
eranga262154 is offline Offline
185 posts
since Sep 2007
Sep 5th, 2007
0

Re: Read a binary file

So, what should I do? Can you explain a little more.
What do you want to do? Read More.
Last edited by vishesh; Sep 5th, 2007 at 4:14 am.
Reputation Points: 85
Solved Threads: 42
Nearly a Posting Virtuoso
vishesh is offline Offline
1,362 posts
since Oct 2006
Sep 5th, 2007
0

Re: Read a binary file

Reputation Points: 10
Solved Threads: 2
Newbie Poster
galin is offline Offline
10 posts
since Aug 2007
Sep 5th, 2007
0

Re: Read a binary file

Thanks for all the links.
Reputation Points: 32
Solved Threads: 2
Junior Poster
eranga262154 is offline Offline
185 posts
since Sep 2007
Sep 5th, 2007
0

Re: Read a binary file

I wrote one as follows.

string line;
	ifstream fileread ("path to the binary file");

	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 not string 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())
Moderator
Reputation Points: 3278
Solved Threads: 890
Posting Sage
WaltP is offline Offline
7,718 posts
since May 2006
Sep 5th, 2007
0

Re: Read a binary file

Quote ...
1: Binary data is not string data. You should probably be using a char array.
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();
Reputation Points: 180
Solved Threads: 34
Posting Whiz
Hamrick is offline Offline
322 posts
since Jun 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