Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string>
- #include <cstring>
- #include <iostream>
- #include <cstdio>
- #include <fstream>
- #include "json.hpp"
- using namespace std;
- using json = nlohmann::json;
- class Account
- {
- private:
- string FIO;
- long INN;
- string password;
- public:
- Account(string FIO, long INN, string password) {
- this->FIO = FIO;
- this->INN = INN;
- this->password = password;
- }
- ~Account(){
- return;
- }
- json serialize() {
- json tmp;
- tmp["FIO"] = this->FIO;
- tmp["INN"] = this->INN;
- tmp["password"] = this->password;
- return tmp;
- }
- };
- void dropOneObj() {
- fstream myfile1 ("test.json", ios_base::ate|ios_base::in|ios_base::binary );
- if (myfile1.is_open())
- {
- Account user1 = Account("Раннев Дмитрий", 13246985, "пароль123");
- json obj1 = user1.serialize();
- myfile1.seekg(-1, ios_base::end);
- myfile1 << "," << obj1;
- myfile1.close();
- }
- }
- void dropAll() {
- ofstream myfile1 ("test.json");
- if (myfile1.is_open())
- {
- Account user1 = Account("Раннев Дмитрий", 13246985, "пароль123");
- Account user2 = Account("Лодж Анастасия", 13246985, "пароль123");
- Account user3 = Account("Коморов Владимир", 13246985, "пароль123");
- Account user4 = Account("Альберт Ошанский", 13246985, "пароль123");
- json obj1 = user1.serialize();
- json obj2 = user2.serialize();
- json obj3 = user3.serialize();
- json obj4 = user4.serialize();
- json arr;
- arr.push_back(obj1);
- arr.push_back(obj2);
- arr.push_back(obj3);
- arr.push_back(obj4);
- myfile1 << arr;
- myfile1.close();
- }
- }
- int main()
- {
- //dropAll();
- dropOneObj();
- json a;
- ifstream myfile ("test.json");
- if (myfile.is_open()) { myfile >> a; }
- myfile.close();
- cout << a.dump() << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement