Guest User

Untitled

a guest
May 24th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iterator>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10. basic_fstream<uint32_t> file( "somefile.dat", ios::in|ios::binary );
  11.  
  12. vector<uint32_t> buffer;
  13. buffer.assign( istream_iterator<uint32_t, uint32_t>( file ), istream_iterator<uint32_t, uint32_t>() );
  14.  
  15. cout << buffer.size() << endl;
  16.  
  17. return 0;
  18. }
  19.  
  20. using namespace std;
  21.  
  22. struct x {};
  23. namespace std {
  24. template<class traits>
  25. class basic_istream<x, traits> : public basic_ifstream<uint32_t>
  26. {
  27. public:
  28. explicit basic_istream<x, traits>(const wchar_t* _Filename,
  29. ios_base::openmode _Mode,
  30. int _Prot = (int)ios_base::_Openprot) : basic_ifstream<uint32_t>( _Filename, _Mode, _Prot ) {}
  31.  
  32. basic_istream<x, traits>& operator>>(uint32_t& data)
  33. {
  34. read(&data, 1);
  35. return *this;
  36. }
  37. };
  38. } // namespace std
  39.  
  40. int main()
  41. {
  42. basic_istream<x> file( "somefile.dat", ios::in|ios::binary );
  43. vector<uint32_t> buffer;
  44. buffer.assign( istream_iterator<uint32_t, x>( file ), istream_iterator<uint32_t, x>() );
  45. cout << buffer.size() << endl;
  46. return 0;
  47. }
  48.  
  49. #include <fstream>
  50. #include <iterator>
  51. #include <vector>
  52. #include <iostream>
  53.  
  54. struct rint // this class will allow us to read binary
  55. {
  56. // ctors & assignment op allows implicit construction from uint
  57. rint () {}
  58. rint (unsigned int v) : val(v) {}
  59. rint (rint const& r) : val(r.val) {}
  60. rint& operator= (rint const& r) { this->val = r.val; return *this; }
  61. rint& operator= (unsigned int r) { this->val = r; return *this; }
  62.  
  63. unsigned int val;
  64.  
  65. // implicit conversion to uint from rint
  66. operator unsigned int& ()
  67. {
  68. return this->val;
  69. }
  70. operator unsigned int const& () const
  71. {
  72. return this->val;
  73. }
  74. };
  75.  
  76. // reads a uints worth of chars into an rint
  77. std::istream& operator>> (std::istream& is, rint& li)
  78. {
  79. is.read(reinterpret_cast<char*>(&li.val), 4);
  80. return is;
  81. }
  82.  
  83. // writes a uints worth of chars out of an rint
  84. std::ostream& operator<< (std::ostream& os, rint const& li)
  85. {
  86. os.write(reinterpret_cast<const char*>(&li.val), 4);
  87. return os;
  88. }
  89.  
  90. int main (int argc, char *argv[])
  91. {
  92. std::vector<int> V;
  93.  
  94. // make sure the file is opened binary & the istream-iterator is
  95. // instantiated with rint; then use the usual copy semantics
  96. std::ifstream file(argv[1], std::ios::binary | std::ios::in);
  97. std::istream_iterator<rint> iter(file), end;
  98. std::copy(iter, end, std::back_inserter(V));
  99.  
  100. for (int i = 0; i < V.size(); ++i)
  101. std::cout << std::hex << "0x" << V[i] << std::endl;
  102.  
  103. // this will reverse the binary file at the uint level (on x86 with
  104. // g++ this is 32-bits at a time)
  105. std::ofstream of(argv[2], std::ios::binary | std::ios::out);
  106. std::ostream_iterator<rint> oter(of);
  107. std::copy(V.rbegin(), V.rend(), oter);
  108.  
  109. return 0;
  110. }
  111.  
  112. std::istream& operator>>(std::istream& in, uint32_t& data)
  113. {
  114. in.read(&data, sizeof(data));
  115. return in;
  116. }
Add Comment
Please, Sign In to add comment