/
Conto.c
78 lines (71 loc) · 2.28 KB
/
Conto.c
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
#include "Conto.h"
#include <stdio.h>
#include <stdlib.h>
#define MAX_UTENTI 20
#define ANSI_COLOR_RED "\x1b[31m" // sconfitta
#define ANSI_COLOR_GREEN "\x1b[32m" // vittoria
#define ANSI_COLOR_YELLOW "\x1b[33m" // info
#define ANSI_COLOR_MAGENTA "\x1b[35m" // banco
#define ANSI_COLOR_CYAN "\x1b[36m" // utente
#define ANSI_COLOR_RESET "\x1b[0m" // reset
int creaidgiocatore();
void impostaconto(Conto* contopersona){
printf(ANSI_COLOR_YELLOW "\nQuanto vuoi caricare sul tuo conto? Solo cifre intere\n-" ANSI_COLOR_RESET);
int valore;
scanf("%d", &valore);
contopersona->ammontare = valore;
contopersona->id = creaidgiocatore();
char nomefile[MAX_UTENTI];
sprintf(nomefile, "data/%d.txt", contopersona->id);
FILE *file = fopen(nomefile, "w");
fprintf(file, "%d", contopersona->ammontare);
fclose(file);
}
void recuperaconto(Conto* contopersona){
FILE *file = NULL;
do{
printf(ANSI_COLOR_YELLOW "\nInserisci l'ID\n-" ANSI_COLOR_RESET);
int recuperaid = 1;
scanf("%d", &recuperaid);
contopersona->id = recuperaid;
char nomefile[MAX_UTENTI];
sprintf(nomefile, "data/%d.txt", contopersona->id);
file = fopen(nomefile, "r");
} while (file == NULL);
fscanf(file, "%d", &contopersona->ammontare);
fclose(file);
}
void aggiornaammontare(Conto* contopersona, int nuovovalore){
char nomefile[MAX_UTENTI];
int ammontarevecchio;
sprintf(nomefile, "data/%d.txt", contopersona->id);
FILE *fileleggi = fopen(nomefile, "r");
fscanf(fileleggi, "%d", &ammontarevecchio);
fclose(fileleggi);
contopersona->ammontare = (ammontarevecchio + nuovovalore);
FILE *filescrivi = fopen(nomefile, "w");
fprintf(filescrivi, "%d", contopersona->ammontare);
fclose(filescrivi);
}
int ottieniid(Conto* contopersona){
return contopersona->id;
}
int ottieniammontare(Conto* contopersona){
return contopersona->ammontare;
}
int creaidgiocatore(){
FILE *file = fopen("data/id.txt", "r+");
int count = 1;
if (file == NULL) {
file = fopen("data/id.txt", "w");
fputc('1', file);
} else {
while (fgetc(file) != EOF) {
count++;
}
fseek(file, 0, SEEK_END);
fputc('1', file);
}
fclose(file);
return count;
}