Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- template <typename T>
- void read(std::istream &stream, T &ref)
- {
- stream.read(reinterpret_cast<char*>(&ref), sizeof(T));
- }
- template <typename T>
- void write(std::ostream &stream, const T &ref)
- {
- stream.write(reinterpret_cast<const char*>(&ref), sizeof(T));
- }
- int main()
- {
- {
- std::fstream file("test.bin", std::fstream::out | std::fstream::binary);
- int i = 42;
- write(file, i);
- } // will close file
- {
- std::fstream file("test.bin", std::fstream::in | std::fstream::binary);
- int i;
- read(file, i);
- std::cout << "i = " << i << std::endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement