Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <fstream>
- #include <map>
- #include <type_traits>
- #include <vector>
- #include <iostream>
- class BinaryOutput
- {
- public:
- BinaryOutput(const std::string &file_name) : file(file_name, std::ios::binary) {};
- template<class T>
- BinaryOutput& operator<<(const T &value)
- {
- write(file, value);
- return *this;
- }
- private:
- std::ofstream file;
- template<class T, typename std::enable_if<std::is_same<T,
- std::vector< typename T::value_type,
- typename T::allocator_type >>::value>::type* = nullptr>
- static void write(std::ostream &stream, const T &container)
- {
- const std::size_t size = container.size();
- stream.write(reinterpret_cast<const char*>(&size), sizeof(std::size_t));
- for (const auto &elem : container)
- {
- write(stream, elem);
- }
- }
- template<class T,
- typename std::enable_if< std::is_fundamental<T>::value >::type* = nullptr>
- static void write(std::ostream &stream, const T &fundamental)
- {
- stream.write(reinterpret_cast<const char*>(&fundamental), sizeof(fundamental));
- }
- static void write(std::ostream &stream, const std::string &str)
- {
- const std::size_t size = str.size();
- stream.write(reinterpret_cast<const char*>(&size), sizeof(std::size_t));
- stream.write(str.c_str(), size);
- }
- };
- class BinaryInput
- {
- public:
- BinaryInput(const std::string &file_name) : file(file_name, std::ios::binary) {};
- template<class T>
- BinaryInput& operator>>(T &value)
- {
- read(file, value);
- return *this;
- }
- private:
- std::ifstream file;
- template<class T, typename std::enable_if<std::is_same<T,
- std::vector< typename T::value_type,
- typename T::allocator_type >>::value>::type* = nullptr>
- static void read(std::istream &stream, T &container)
- {
- std::size_t size;
- stream.read(reinterpret_cast<char*>(&size), sizeof(std::size_t));
- container.resize(size);
- for (auto &elem : container)
- {
- read(stream, elem);
- }
- }
- template<class T,
- typename std::enable_if< std::is_fundamental<T>::value >::type* = nullptr>
- static void read(std::istream &stream, T &fundamental)
- {
- stream.read(reinterpret_cast<char*>(&fundamental), sizeof(fundamental));
- }
- static void read(std::istream &stream, std::string &str)
- {
- std::size_t size;
- stream.read(reinterpret_cast<char*>(&size), sizeof(std::size_t));
- str.resize(size);
- stream.read(const_cast<char*>(str.c_str()), size);
- }
- };
- int main()
- {
- {
- BinaryOutput out("bin");
- int val1 = 10;
- double val2 = 20;
- float val3 = 30;
- long val4 = 40;
- out << val1 << val2 << val3 << val4;
- }
- {
- BinaryInput in("bin");
- int val1;
- double val2;
- float val3;
- long val4;
- in >> val1 >> val2 >> val3 >> val4;
- std::cout << "Fundamentals:\n" << val1 << '\n' << val2 << '\n' << val3 << '\n' << val4 << std::endl;
- }
- {
- BinaryOutput out("bin");
- std::vector<int> val1{ 1, 2, 3 };
- std::vector<double> val2{ 4.42342344, 5.654654654635, 6.4234234125 };
- std::vector<float> val3{ 7.634f, 8.12321f, 9.56546f };
- std::vector<std::string> val4{ "lol", "kek" , "ololo" };
- out << val1 << val2 << val3 << val4;
- }
- {
- BinaryInput out("bin");
- std::vector<int> val1;
- std::vector<double> val2;
- std::vector<float> val3;
- std::vector<std::string> val4;
- out >> val1 >> val2 >> val3 >> val4;
- std::cout << "Vectors:\n";
- for (int i = 0; i < val4.size(); ++i)
- {
- std::cout << val1[i] << ' ' << val2[i] << ' ' << val3[i] << ' ' << val4[i] << std::endl;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement