-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArchivoCliente.cpp
More file actions
105 lines (91 loc) · 2.42 KB
/
Copy pathArchivoCliente.cpp
File metadata and controls
105 lines (91 loc) · 2.42 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "ArchivoCliente.h"
bool ArchivoCliente::guardar(Cliente reg) {
FILE *pFile = fopen("cliente.dat", "ab");
if (pFile == nullptr) {
return false;
}
bool result = fwrite(®, sizeof(Cliente), 1, pFile);
fclose(pFile);
return result;
}
bool ArchivoCliente::guardar(int index, Cliente reg) {
FILE *pFile = fopen("cliente.dat", "rb+");
if (pFile == nullptr) {
return false;
}
fseek(pFile, sizeof(Cliente) * index, SEEK_SET);
bool result = fwrite(®, sizeof(Cliente), 1, pFile);
fclose(pFile);
return result;
}
int ArchivoCliente::buscarByID(int id) {
Cliente reg;
int pos = 0;
FILE *pFile = fopen("cliente.dat", "rb");
if (pFile == nullptr) {
return -1;
}
while (fread(®, sizeof(Cliente), 1, pFile)) {
if (reg.getIdCliente() == id) {
fclose(pFile);
return pos;
}
pos++;
}
fclose(pFile);
return -1;
}
int ArchivoCliente::buscarByDni(int dni) {
Cliente reg;
int pos = 0;
FILE *pFile = fopen("cliente.dat", "rb");
if (pFile == nullptr) {
return -1;
}
while (fread(®, sizeof(Cliente), 1, pFile)) {
if (reg.getDni() == dni) {
fclose(pFile);
return reg.getIdCliente();
}
pos++;
}
fclose(pFile);
return -1;
}
Cliente ArchivoCliente::leerCliente(int index) {
Cliente reg;
FILE *pFile = fopen("cliente.dat", "rb");
if (pFile == nullptr) {
return reg;
}
fseek(pFile, index * sizeof(Cliente), SEEK_SET);
fread(®, sizeof(Cliente), 1, pFile);
fclose(pFile);
return reg;
}
void ArchivoCliente::leerTodos(Cliente registros[], int cantidad) {
FILE *pFile = fopen("cliente.dat", "rb");
if (pFile == nullptr) {
return;
}
fread(registros, sizeof(Cliente), cantidad, pFile);
fclose(pFile);
}
int ArchivoCliente::getCantidadRegistros() {
FILE *pFile = fopen("cliente.dat", "rb");
if (pFile == nullptr) {
return 0;
}
fseek(pFile, 0, SEEK_END);
int tam = ftell(pFile) / sizeof(Cliente);
fclose(pFile);
return tam;
}
int ArchivoCliente::getNuevoID() {
int cantidad = getCantidadRegistros();
if (cantidad > 0) {
return leerCliente(cantidad - 1).getIdCliente() + 1;
} else {
return 1;
}
}