Révisions
This commit is contained in:
80
Révisions/Fichiers/challenger.c
Normal file
80
Révisions/Fichiers/challenger.c
Normal 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");
|
||||
}
|
||||
18
Révisions/Fichiers/copie.c
Normal file
18
Révisions/Fichiers/copie.c
Normal 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);
|
||||
}
|
||||
}
|
||||
19
Révisions/Fichiers/records.c
Normal file
19
Révisions/Fichiers/records.c
Normal 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
BIN
Révisions/Fichiers/taupe10
Normal file
Binary file not shown.
BIN
Révisions/Fichiers/top10
Normal file
BIN
Révisions/Fichiers/top10
Normal file
Binary file not shown.
Reference in New Issue
Block a user