Dynamic array

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

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

Read a binary file

 
0
  #1
Sep 5th, 2007
Can someone explain, how can read a binary file. I'm really nervous with this.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,311
Reputation: vishesh is on a distinguished road 
Solved Threads: 36
vishesh's Avatar
vishesh vishesh is offline Offline
Nearly a Posting Virtuoso

Re: Read a binary file

 
0
  #2
Sep 5th, 2007
Google it and you'll get loads of good articles on that:
http://www.google.com/search?ie=UTF-...les+in+c%2B%2B
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
  #3
Sep 5th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,311
Reputation: vishesh is on a distinguished road 
Solved Threads: 36
vishesh's Avatar
vishesh vishesh is offline Offline
Nearly a Posting Virtuoso

Re: Read a binary file

 
0
  #4
Sep 5th, 2007
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.
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
  #5
Sep 5th, 2007
So, what should I do? Can you explain a little more.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 1,311
Reputation: vishesh is on a distinguished road 
Solved Threads: 36
vishesh's Avatar
vishesh vishesh is offline Offline
Nearly a Posting Virtuoso

Re: Read a binary file

 
0
  #6
Sep 5th, 2007
Originally Posted by eranga262154 View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 10
Reputation: galin is an unknown quantity at this point 
Solved Threads: 2
galin galin is offline Offline
Newbie Poster

Re: Read a binary file

 
0
  #7
Sep 5th, 2007
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
  #8
Sep 5th, 2007
Thanks for all the links.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Read a binary file

 
0
  #9
Sep 5th, 2007
Originally Posted by eranga262154 View Post
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())
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
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
  #10
Sep 5th, 2007
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.
  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();
The truth does not change according to our ability to stomach it.
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