read & write Binary data

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2006
Posts: 16
Reputation: mhm_ra is an unknown quantity at this point 
Solved Threads: 0
mhm_ra mhm_ra is offline Offline
Newbie Poster

read & write Binary data

 
0
  #1
May 25th, 2006
hi
i need to r write binary(hexadecimal) values into a text file then read this data and manipulate it as a number
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: read & write Binary data

 
0
  #2
May 25th, 2006
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?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 16
Reputation: mhm_ra is an unknown quantity at this point 
Solved Threads: 0
mhm_ra mhm_ra is offline Offline
Newbie Poster

Re: read & write Binary data

 
0
  #3
May 25th, 2006
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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,335
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 236
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: read & write Binary data

 
0
  #4
May 25th, 2006
  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. */
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
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