943,568 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1980
  • C++ RSS
Aug 22nd, 2008
0

how to read a binary file data to a monitor

Expand Post »
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.

  
C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mksakeesh is offline Offline
17 posts
since Aug 2008
Aug 22nd, 2008
0

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

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.
Reputation Points: 395
Solved Threads: 71
Posting Whiz
jencas is offline Offline
362 posts
since Dec 2007
Aug 22nd, 2008
0

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

You would also need to delete the memory you've freed, or else you'll get a memory leak
Moderator
Featured Poster
Reputation Points: 4142
Solved Threads: 394
Industrious Poster
Nick Evan is offline Offline
4,132 posts
since Oct 2006
Aug 22nd, 2008
0

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

Both suffer from having void main as well.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

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: MouseMove in Visual C++
Next Thread in C++ Forum Timeline: Help with creating a class





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC