80 lines
1.9 KiB
C
80 lines
1.9 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
typedef struct joueur {
|
|
int score;
|
|
char nom[4];
|
|
} joueur;
|
|
|
|
joueur* readTop(char* filename) {
|
|
joueur* classement = (joueur*)calloc(10, sizeof(joueur));
|
|
|
|
FILE* f = fopen(filename, "r");
|
|
|
|
if (f) {
|
|
for (int i = 0; i < 10; i++) {
|
|
if (!feof(f)) {
|
|
int score;
|
|
char nom[4];
|
|
fread(&score, sizeof(int), 1, f);
|
|
fread(nom, sizeof(char), 3, f);
|
|
classement[i].score = score;
|
|
strcpy(classement[i].nom, nom);
|
|
} else break;
|
|
}
|
|
fclose(f);
|
|
}
|
|
|
|
return classement;
|
|
}
|
|
|
|
void showTop(char* filename) {
|
|
FILE* f = fopen(filename, "r");
|
|
|
|
if (f) {
|
|
for (int i = 0; i < 10; i++) {
|
|
if (!feof(f)) {
|
|
int score;
|
|
char nom[4];
|
|
fread(&score, sizeof(int), 1, f);
|
|
fread(nom, sizeof(char), 3, f);
|
|
printf("%09d %s\n", score, nom);
|
|
} else break;
|
|
}
|
|
fclose(f);
|
|
}
|
|
}
|
|
|
|
void writeScore(char* filename, char* nom, int score) {
|
|
joueur* classement = readTop(filename);
|
|
|
|
for (int i = 0; i < 10; i++) {
|
|
if (classement[i].score < score) {
|
|
for (int j = i; j < 9; j++) {
|
|
classement[j+1] = classement[j];
|
|
}
|
|
|
|
classement[i].score = score;
|
|
strcpy(classement[i].nom, nom);
|
|
break;
|
|
}
|
|
}
|
|
|
|
FILE* f = fopen(filename, "w");
|
|
|
|
if (f) {
|
|
for (int i = 0; i < 10; i++) {
|
|
fwrite(&(classement[i].score), sizeof(int), 1, f);
|
|
fwrite(classement[i].nom, sizeof(char), 3, f);
|
|
}
|
|
|
|
fclose(f);
|
|
}
|
|
}
|
|
|
|
int main(int argc, char* argv[]) {
|
|
int score = (int)strtol(argv[1], NULL, 10);
|
|
writeScore("top10", argv[2], score);
|
|
showTop("top10");
|
|
} |