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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
typedef struct nodmain //some blocks with the information i want to save
{
nod *nextright;
nodmain *nextdown;
nodmain *prev;
int num;
bool mine;
bool reveal;
};
typedef struct nod
{
nodmain *prevmain;
nod *prevnod;
nod *nextright;
int num;
bool mine;
bool reveal;
};
//functions to initialize the structs so i can be lazy and just call them :D
nod *Inicializa() //me being lazy
{
nod *init = (nod*)malloc(sizeof(nod));
return init;
}
nodmain *Inicializamain() //me being lazy x2
{
nodmain *init = (nodmain*)malloc(sizeof(nodmain));
return init;
}
nodmain *Load(int size)
{
FILE *Load = fopen("SavedGame.txt", "r"); //the text already exists
nodmain *auxmain = Inicializamain();
nodmain *header = auxmain;
char boolstringmine[10];
char boolstringreveal[10];
for (int i = 0; i < size; i++)
{
fscanf(Load, "%d,%s,%s\t", auxmain->num, &boolstringmine, &boolstringreveal); //ERROR here
if (boolstringmine == "true")
{
auxmain->mine = true;
}
else
{
auxmain->mine = false;
}
if (boolstringreveal == "true")
{
auxmain->reveal = true;
}
else
{
auxmain->reveal = false;
}
nod *aux = Inicializa();
auxmain->nextright = aux;
aux->prevmain = auxmain;
aux->prevnod = NULL;
for (int j = 0; j < (size - 2); j++)
{
fscanf(Load, "%d,%s,%s\t", aux->num, &boolstringmine, &boolstringreveal);
if (boolstringmine == "true")
{
aux->mine = true;
}
else
{
aux->mine = false;
}
if (boolstringreveal == "true")
{
aux->reveal = true;
}
else
{
aux->reveal = false;
}
nod *nextnod = Inicializa();
nextnod->prevnod = aux;
nextnod->prevmain = NULL;
aux->nextright = aux;
aux = aux->nextright;
}
nodmain *nextmain = Inicializamain();
auxmain->nextdown = nextmain;
nextmain->prev = auxmain;
auxmain = nextmain;
fscanf(Load, "\n");
}
fclose(Load);
return header;
}
//this function is the one that writes on the text file, works perfectly.
void Save(nodmain *header)
{
FILE *Save = fopen("SavedGame.txt", "w");
nodmain *auxmain = header;
nod *aux = Inicializa();
if (Save == NULL)
{
printf("ERROR");
}
else
{
while (auxmain != NULL)
{
fprintf(Save, "%d,%s,%s\t", auxmain->num, auxmain->mine ? "true" : "false", auxmain->reveal ? "true" : "false");
aux = auxmain->nextright;
while (aux != NULL)
{
fprintf(Save, "%d,%s,%s\t", aux->num, aux->mine ? "true" : "false", aux->reveal ? "true" : "false");
aux = aux->nextright;
}
fprintf(Save, "\n");
auxmain = auxmain->nextdown;
}
}
fclose(Save);
}
|