-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.h
More file actions
46 lines (40 loc) · 1.43 KB
/
Client.h
File metadata and controls
46 lines (40 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#pragma once
#ifndef CLIENT_H
#define CLIENT_H
#include<iostream>
#include<fstream>
using namespace std;
class Client {
public:
string name;
string secondName;
Client() {
name = "unknown";
secondName = "unknown";
}
Client(string n, string sn) : name(n), secondName(sn) {}
void saveToFile(std::ofstream& outFile) const {
size_t nameLength = name.size();
outFile.write(reinterpret_cast<const char*>(&nameLength), sizeof(nameLength));
outFile.write(name.c_str(), nameLength);
size_t secondNameLength = secondName.size();
outFile.write(reinterpret_cast<const char*>(&secondNameLength), sizeof(secondNameLength));
outFile.write(secondName.c_str(), secondNameLength);
}
void loadFromFile(std::ifstream& inFile) {
size_t nameLength, secondNameLength;
inFile.read(reinterpret_cast<char*>(&nameLength), sizeof(nameLength));
char* buffer1 = new char[nameLength + 1];
inFile.read(buffer1, nameLength);
buffer1[nameLength] = '\0';
name = std::string(buffer1);
delete[] buffer1;
inFile.read(reinterpret_cast<char*>(&secondNameLength), sizeof(secondNameLength));
char* buffer2 = new char[secondNameLength + 1];
inFile.read(buffer2, secondNameLength);
buffer2[secondNameLength] = '\0';
secondName = std::string(buffer2);
delete[] buffer2;
}
};
#endif