Advertisement
Guest User

Untitled

a guest
Dec 15th, 2015
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. using namespace std;
  4.  
  5. struct test {
  6.     int integer;
  7.     float floating;
  8. };
  9.  
  10. void save(const char* path, const test& source) {
  11.     fstream file(path, ios::out | ios::binary);
  12.     if (file.is_open()) {
  13.         file.write((char*)&source, sizeof(source));
  14.         file.close();
  15.     }
  16. }
  17.  
  18. void load(const char* path, test& destination) {
  19.     fstream file(path, ios::in | ios::binary);
  20.     if (file.is_open()) {
  21.         file.read((char*)&destination, sizeof(destination));
  22.         file.close();
  23.     }
  24. }
  25.  
  26. int main() {
  27.     test testData;
  28.     testData.integer = 12;
  29.     testData.floating = 1.4f;
  30.     save("test.bin", testData);
  31.  
  32.     test loaded;
  33.     load("test.bin", loaded);
  34.     cout << "Int: " << loaded.integer << " Float: " << loaded.floating << endl;
  35.     return 0;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement