RSS Forums RSS

how to read a binary file data to a monitor

Please support our C++ advertiser: Programming Forums
Reply
Posts: 13
Reputation: mksakeesh is an unknown quantity at this point 
Solved Threads: 0
mksakeesh mksakeesh is offline Offline
Newbie Poster

how to read a binary file data to a monitor

  #1  
Aug 22nd, 2008
Please compare the two codes below why is the first one displaying output to monitor where as the second code is displaying some unwanted characters.

  1. CODE 1 .....
  2. #include <fstream.h>
  3. #include <string.h>
  4.  
  5. void main()
  6. {
  7. fstream File("test_file.txt",ios::out | ios::in | ios::binary );
  8.  
  9. char arr[13];
  10. strcpy(arr,"Hello World!"); //put Hello World! into the array
  11.  
  12. File.write(arr,5); //put the first 5 symbols into the file- "Hello"
  13.  
  14. static char read_array[10]; //I will put the read data, here
  15.  
  16. File.seekg(ios::beg);
  17. File.read(read_array,5); //read the first 3 symbols- "Hel"
  18.  
  19. cout << read_array << endl; //display them
  20.  
  21. File.close();
  22. }
  23.  
  24. CODE 2....
  25. #include <fstream.h>
  26. #include <string.h>
  27.  
  28. void main()
  29. {
  30. fstream File("test_file.txt",ios::out | ios::in | ios::binary );
  31. char * memblock;
  32. ifstream::pos_type size;
  33. //int size;
  34.  
  35. File.seekg(ios::ate);
  36. size = File.tellg();
  37. memblock = new char[size];
  38. File.read(memblock,size);
  39.  
  40. //cout<<*memblock;
  41. //cout<<"\n";
  42. cout<<memblock;
  43.  
  44. File.close();
  45. }
AddThis Social Bookmark Button
Reply With Quote  
Posts: 285
Reputation: jencas is a jewel in the rough jencas is a jewel in the rough jencas is a jewel in the rough jencas is a jewel in the rough 
Solved Threads: 53
jencas jencas is offline Offline
Posting Whiz in Training

Re: how to read a binary file data to a monitor

  #2  
Aug 22nd, 2008
CODE 1 may end in problems, too, because of the missing terminating 0 in read_array

CODE 2 is missing a rewind to begin of file before File.read(memblock,size); and has the same problem with the terminating 0. Replace memblock = new char[size]; by memblock = new char[size + 1]; memblock[size] = 0;
Last edited by jencas : Aug 22nd, 2008 at 10:27 am.
If you are forced to reinvent the wheel at least try to invent a better one!

Please help us helping you and read carefully: http://www.daniweb.com/forums/thread78223.html
Reply With Quote  
Posts: 2,362
Reputation: niek_e has much to be proud of niek_e has much to be proud of niek_e has much to be proud of niek_e has much to be proud of niek_e has much to be proud of niek_e has much to be proud of niek_e has much to be proud of niek_e has much to be proud of niek_e has much to be proud of niek_e has much to be proud of 
Solved Threads: 256
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Nearly a Posting Maven

Re: how to read a binary file data to a monitor

  #3  
Aug 22nd, 2008
You would also need to delete the memory you've freed, or else you'll get a memory leak
Want better/more replies to your questions? Wrap your code in [code] [/code] tags!
Reply With Quote  
Posts: 5,133
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 634
Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: how to read a binary file data to a monitor

  #4  
Aug 22nd, 2008
Both suffer from having void main as well.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
UK Voter? Please send a message to Incapability Brown and the rest of Zanu-Labour
Up to 8Mb PlusNet broadband from only £5.99 a month!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Similar Threads
Other Threads in the C++ Forum
Views: 1075 | Replies: 3 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 2:37 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC