Advertisement
bueddl

Untitled

Aug 4th, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. template <typename T>
  5. void read(std::istream &stream, T &ref)
  6. {
  7.     stream.read(reinterpret_cast<char*>(&ref), sizeof(T));
  8. }
  9.  
  10. template <typename T>
  11. void write(std::ostream &stream, const T &ref)
  12. {
  13.     stream.write(reinterpret_cast<const char*>(&ref), sizeof(T));
  14. }
  15.  
  16. int main()
  17. {
  18.     {
  19.         std::fstream file("test.bin", std::fstream::out | std::fstream::binary);
  20.  
  21.         int i = 42;
  22.         write(file, i);
  23.     } // will close file
  24.  
  25.     {
  26.         std::fstream file("test.bin", std::fstream::in | std::fstream::binary);
  27.  
  28.         int i;
  29.         read(file, i);
  30.         std::cout << "i = " << i << std::endl;
  31.     }
  32.  
  33.     return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement