944,028 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 23171
  • C++ RSS
May 25th, 2006
0

read & write Binary data

Expand Post »
hi
i need to r write binary(hexadecimal) values into a text file then read this data and manipulate it as a number
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mhm_ra is offline Offline
16 posts
since Mar 2006
May 25th, 2006
0

Re: read & write Binary data

Quote originally posted by mhm_ra ...
i need to r write binary(hexadecimal) values into a text file then read this data and manipulate it as a number
So is it binary or is it text? Do you mean that you want to write the binary text representation of a value and read this text back as its value?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
May 25th, 2006
0

Re: read & write Binary data

i write a hexadecimal value on a file using "ofstream" then i want to read this value from a file (may be as a string) and then convert it to its hexadecimal value then manipulate it(i.e add,subtract...........)
so can i do that?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mhm_ra is offline Offline
16 posts
since Mar 2006
May 25th, 2006
0

Re: read & write Binary data

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. void write(const char *filename)
  5. {
  6. std::ofstream file(filename);
  7. for ( int i = 100; i < 110; ++i )
  8. {
  9. file << std::hex << i << '\n';
  10. }
  11. }
  12.  
  13. void read(const char *filename)
  14. {
  15. std::ifstream file(filename);
  16. int value;
  17. while ( file >> std::hex >> value )
  18. {
  19. std::cout << "value = " << value << '\n';
  20. }
  21. }
  22.  
  23. int main()
  24. {
  25. static const char filename[] = "file.txt";
  26. write(filename);
  27. read(filename);
  28. return 0;
  29. }
  30.  
  31. /* my output
  32. value = 100
  33. value = 101
  34. value = 102
  35. value = 103
  36. value = 104
  37. value = 105
  38. value = 106
  39. value = 107
  40. value = 108
  41. value = 109
  42. */
  43.  
  44. /* file.txt
  45. 64
  46. 65
  47. 66
  48. 67
  49. 68
  50. 69
  51. 6a
  52. 6b
  53. 6c
  54. 6d
  55. */
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

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: trie
Next Thread in C++ Forum Timeline: Debug





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


Follow us on Twitter


© 2011 DaniWeb® LLC