View Single Post
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