how to read a binary file data to a monitor

Reply

Join Date: Aug 2008
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

 
0
  #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. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 359
Reputation: jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice 
Solved Threads: 69
jencas jencas is offline Offline
Posting Whiz

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

 
0
  #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 11:27 am.
If you are forced to reinvent the wheel at least try to invent a better one!

Please use code tags - Please mark solved threads as solved
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,753
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 294
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Posting Maven

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

 
0
  #3
Aug 22nd, 2008
You would also need to delete the memory you've freed, or else you'll get a memory leak
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
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: 749
Team 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

 
0
  #4
Aug 22nd, 2008
Both suffer from having void main as well.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC