Révisions

This commit is contained in:
2022-01-25 12:00:33 +01:00
parent c30ba3ccdf
commit 64944874bc
14 changed files with 492 additions and 0 deletions

View File

@@ -0,0 +1,80 @@
#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");
}

View File

@@ -0,0 +1,18 @@
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[]) {
FILE* f = fopen(argv[1], "r");
FILE* f2 = fopen(argv[2], "w");
if (f && f2) {
while (!feof(f)) {
char c;
fread(&c, sizeof(char), 1, f);
fwrite(&c, sizeof(char), 1, f2);
}
fclose(f);
fclose(f2);
}
}

View File

@@ -0,0 +1,19 @@
#include <stdio.h>
#include <stdlib.h>
int main(void) {
FILE* f = fopen("top10", "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);
}
}

BIN
Révisions/Fichiers/taupe10 Normal file

Binary file not shown.

BIN
Révisions/Fichiers/top10 Normal file

Binary file not shown.