Advertisement
DmitryPythonDevelop

Untitled

Apr 6th, 2020
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.84 KB | None | 0 0
  1. #include <string>
  2. #include <cstring>
  3. #include <iostream>
  4. #include <cstdio>
  5. #include <fstream>
  6. #include "json.hpp"
  7.  
  8. using namespace std;
  9. using json = nlohmann::json;
  10.  
  11. class Account
  12. {
  13.     private:
  14.         string FIO;
  15.         long INN;
  16.         string password;
  17.     public:
  18.         Account(string FIO, long INN, string password) {
  19.             this->FIO = FIO;
  20.             this->INN = INN;
  21.             this->password = password;
  22.         }
  23.         ~Account(){
  24.             return;
  25.         }
  26.  
  27.         json serialize() {
  28.             json tmp;
  29.  
  30.             tmp["FIO"] = this->FIO;
  31.             tmp["INN"] = this->INN;
  32.             tmp["password"] = this->password;
  33.  
  34.             return tmp;
  35.         }
  36. };
  37.  
  38.  
  39. void dropOneObj() {
  40.     fstream myfile1 ("test.json", ios_base::ate|ios_base::in|ios_base::binary );
  41.     if (myfile1.is_open())
  42.     {
  43.         Account user1 = Account("Раннев Дмитрий", 13246985, "пароль123");
  44.         json obj1 = user1.serialize();
  45.  
  46.         myfile1.seekg(-1, ios_base::end);
  47.         myfile1 << "," << obj1;
  48.  
  49.         myfile1.close();
  50.     }
  51. }
  52.  
  53. void dropAll() {
  54.     ofstream myfile1 ("test.json");
  55.     if (myfile1.is_open())
  56.     {
  57.         Account user1 = Account("Раннев Дмитрий", 13246985, "пароль123");
  58.         Account user2 = Account("Лодж Анастасия", 13246985, "пароль123");
  59.         Account user3 = Account("Коморов Владимир", 13246985, "пароль123");
  60.         Account user4 = Account("Альберт Ошанский", 13246985, "пароль123");
  61.  
  62.         json obj1 = user1.serialize();
  63.         json obj2 = user2.serialize();
  64.         json obj3 = user3.serialize();
  65.         json obj4 = user4.serialize();
  66.  
  67.         json arr;
  68.  
  69.         arr.push_back(obj1);
  70.         arr.push_back(obj2);
  71.         arr.push_back(obj3);
  72.         arr.push_back(obj4);
  73.  
  74.         myfile1 << arr;
  75.  
  76.         myfile1.close();
  77.     }
  78. }
  79.  
  80.  
  81. int main()
  82. {
  83.  
  84.  
  85.     //dropAll();
  86.    
  87.     dropOneObj();
  88.  
  89.     json a;
  90.  
  91.     ifstream myfile ("test.json");
  92.     if (myfile.is_open()) { myfile >> a; }
  93.     myfile.close();
  94.  
  95.     cout << a.dump() << endl;
  96.  
  97.  
  98. return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement