Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <iterator>
- #include <vector>
- using namespace std;
- int main()
- {
- basic_fstream<uint32_t> file( "somefile.dat", ios::in|ios::binary );
- vector<uint32_t> buffer;
- buffer.assign( istream_iterator<uint32_t, uint32_t>( file ), istream_iterator<uint32_t, uint32_t>() );
- cout << buffer.size() << endl;
- return 0;
- }
- using namespace std;
- struct x {};
- namespace std {
- template<class traits>
- class basic_istream<x, traits> : public basic_ifstream<uint32_t>
- {
- public:
- explicit basic_istream<x, traits>(const wchar_t* _Filename,
- ios_base::openmode _Mode,
- int _Prot = (int)ios_base::_Openprot) : basic_ifstream<uint32_t>( _Filename, _Mode, _Prot ) {}
- basic_istream<x, traits>& operator>>(uint32_t& data)
- {
- read(&data, 1);
- return *this;
- }
- };
- } // namespace std
- int main()
- {
- basic_istream<x> file( "somefile.dat", ios::in|ios::binary );
- vector<uint32_t> buffer;
- buffer.assign( istream_iterator<uint32_t, x>( file ), istream_iterator<uint32_t, x>() );
- cout << buffer.size() << endl;
- return 0;
- }
- #include <fstream>
- #include <iterator>
- #include <vector>
- #include <iostream>
- struct rint // this class will allow us to read binary
- {
- // ctors & assignment op allows implicit construction from uint
- rint () {}
- rint (unsigned int v) : val(v) {}
- rint (rint const& r) : val(r.val) {}
- rint& operator= (rint const& r) { this->val = r.val; return *this; }
- rint& operator= (unsigned int r) { this->val = r; return *this; }
- unsigned int val;
- // implicit conversion to uint from rint
- operator unsigned int& ()
- {
- return this->val;
- }
- operator unsigned int const& () const
- {
- return this->val;
- }
- };
- // reads a uints worth of chars into an rint
- std::istream& operator>> (std::istream& is, rint& li)
- {
- is.read(reinterpret_cast<char*>(&li.val), 4);
- return is;
- }
- // writes a uints worth of chars out of an rint
- std::ostream& operator<< (std::ostream& os, rint const& li)
- {
- os.write(reinterpret_cast<const char*>(&li.val), 4);
- return os;
- }
- int main (int argc, char *argv[])
- {
- std::vector<int> V;
- // make sure the file is opened binary & the istream-iterator is
- // instantiated with rint; then use the usual copy semantics
- std::ifstream file(argv[1], std::ios::binary | std::ios::in);
- std::istream_iterator<rint> iter(file), end;
- std::copy(iter, end, std::back_inserter(V));
- for (int i = 0; i < V.size(); ++i)
- std::cout << std::hex << "0x" << V[i] << std::endl;
- // this will reverse the binary file at the uint level (on x86 with
- // g++ this is 32-bits at a time)
- std::ofstream of(argv[2], std::ios::binary | std::ios::out);
- std::ostream_iterator<rint> oter(of);
- std::copy(V.rbegin(), V.rend(), oter);
- return 0;
- }
- std::istream& operator>>(std::istream& in, uint32_t& data)
- {
- in.read(&data, sizeof(data));
- return in;
- }
Add Comment
Please, Sign In to add comment