DrGustav

Untitled

Nov 26th, 2020 (edited)
476
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #include <stdio.h>
  3. #include<iostream>
  4. #include<fstream>
  5. using namespace std;
  6.  
  7. class Customer {
  8.   public:
  9.     int code;
  10.     string name;
  11.     void ini(int code, string name);
  12.     int getCode() { return code; }
  13.     string getName() {return name; }
  14. };
  15.  
  16. void Customer::ini(int code, string name) {
  17.   this->code = code;
  18.   this->name = name;
  19. }
  20.  
  21. int main() {
  22.     ofstream wf("clientes.dat", ios::out | ios::binary);
  23.     if(!wf) {
  24.         cout << "Cannot open file!" << endl;
  25.         return 1;
  26.     }
  27.  
  28.     list<Customer> *clientes;
  29.     clientes = new list<Customer>[2];
  30.  
  31.     Customer *cl = new Customer();
  32.     cl->ini(17, "henrique");
  33.     clientes[0].push_back(*cl);
  34.     cl->ini(7, "gustavo");
  35.     clientes[1].push_back(*cl);
  36.  
  37.     for(int i = 0; i < 2; i++) {
  38.         wf.write((char *) &clientes[i], sizeof(clientes[i]));
  39.         cout << i << endl;
  40.     }
  41.     wf.close();
  42.  
  43.     if(!wf.good()) {
  44.         cout << "Error occurred at writing time!" << endl;
  45.         return 1;
  46.     }
  47.  
  48.     cout << "escreveu" << endl;
  49.  
  50.     ifstream rf("clientes.dat", ios::out | ios::binary);
  51.     if(!rf) {
  52.         cout << "Cannot open file!" << endl;
  53.         return 1;
  54.     }
  55.  
  56.     list<Customer> *clientes2;
  57.     clientes2 = new list<Customer>[2];
  58.     for(int i = 0; i < 2; i++) {
  59.         rf.read((char *) &clientes2[i], sizeof(clientes2[i]));
  60.     }
  61.     rf.close();
  62.  
  63.     if(!rf.good()) {
  64.         cout << "Error occurred at reading time!" << endl;
  65.         return 1;
  66.     }
  67.  
  68.     cout<<"Customer's Details:"<<endl;
  69.     for(int i=0; i < 2; i++) {
  70.         cout << i << " ";
  71.         for (auto x : clientes2[i]) {
  72.             cout << x.getName() << endl;
  73.         }
  74.     }
  75.     cout << "fim " << endl;
  76.     return 0;
  77. }
Add Comment
Please, Sign In to add comment