Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- #include <stdio.h>
- #include<iostream>
- #include<fstream>
- using namespace std;
- class Customer {
- public:
- int code;
- string name;
- void ini(int code, string name);
- int getCode() { return code; }
- string getName() {return name; }
- };
- void Customer::ini(int code, string name) {
- this->code = code;
- this->name = name;
- }
- int main() {
- ofstream wf("clientes.dat", ios::out | ios::binary);
- if(!wf) {
- cout << "Cannot open file!" << endl;
- return 1;
- }
- list<Customer> *clientes;
- clientes = new list<Customer>[2];
- Customer *cl = new Customer();
- cl->ini(17, "henrique");
- clientes[0].push_back(*cl);
- cl->ini(7, "gustavo");
- clientes[1].push_back(*cl);
- for(int i = 0; i < 2; i++) {
- wf.write((char *) &clientes[i], sizeof(clientes[i]));
- cout << i << endl;
- }
- wf.close();
- if(!wf.good()) {
- cout << "Error occurred at writing time!" << endl;
- return 1;
- }
- cout << "escreveu" << endl;
- ifstream rf("clientes.dat", ios::out | ios::binary);
- if(!rf) {
- cout << "Cannot open file!" << endl;
- return 1;
- }
- list<Customer> *clientes2;
- clientes2 = new list<Customer>[2];
- for(int i = 0; i < 2; i++) {
- rf.read((char *) &clientes2[i], sizeof(clientes2[i]));
- }
- rf.close();
- if(!rf.good()) {
- cout << "Error occurred at reading time!" << endl;
- return 1;
- }
- cout<<"Customer's Details:"<<endl;
- for(int i=0; i < 2; i++) {
- cout << i << " ";
- for (auto x : clientes2[i]) {
- cout << x.getName() << endl;
- }
- }
- cout << "fim " << endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment