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
| #include <stdio.h>
#include <stdlib.h>
#include "fichier.h"
#include "graphe.h"
void ouvertureFichier (void) {
char entree [50];
FILE* fichier=NULL;
int nbrSommets;
printf ("Quelle fichier voulez-vous ouvrir ?\n");
gets (entree);//Permet de vider le tampon qui contient le \n de la dernière saisie
gets (entree);
char *nomFichier=entree;
fichier=fopen(nomFichier, "a+");
if (fichier!=NULL) {
printf ("L'ouverture du fichier a été un succés.\n");
struct table* graphe=malloc(sizeof(struct table));
graphe->poid=-1;
graphe=lectureFichier (fichier, graphe);
printf ("Le fichier a été lu correctement.\n");
actionsGraphe (graphe);
close(fichier);
}
else {
printf ("Il y a eut un problème lors de l'ouverture du fichier.\n");
}
}
struct table* lectureFichier (FILE* fichier, struct table* graphe) {
int nbrSommets, nbrAretes, sommet1, sommet2, poid, i;
fscanf (fichier, "%d", &nbrSommets);
fseek(fichier, 1, SEEK_CUR);//permet de passer à la ligne 2 pour lire le nombre correspondant au nombre d'arcs
fscanf (fichier, "%d", &nbrAretes);
for (i=0 ; i<nbrAretes ; i++) {
fseek(fichier, 1, SEEK_CUR); //permet de passer à la ligne suivante
fscanf (fichier, "%d %d %d", &sommet1, &sommet2, &poid); //permet de lire la ligne, et de récupperrer son contenue
graphe=insererArete (graphe, sommet1, sommet2, poid);
}
graphe->nbrSommets=nbrSommets;
graphe->nbrAretes=nbrAretes;
return graphe;
}
void ecritureFichier (struct table* graphe) {
int nbrSommets, nbrAretes;
char tampon, *nomFichier;
char entree [50];
FILE* fichier;
nbrSommets=graphe->nbrSommets;
nbrAretes=graphe->nbrAretes;
printf ("Quel est le nom que vous voulez donner au fichier ?\n");
gets (entree);//Permet de vider le tampon qui contient le \n de la dernière saisie
gets (entree);
nomFichier=entree;
fichier=fopen (nomFichier, "w+");
if (fichier!=NULL) {
fprintf (fichier, "%d", nbrSommets); //permet d'écrire le nombre de sommets du graphe
fprintf (fichier, "\n%d", nbrAretes); //permet d'écrire le nombre d'arcs du graphe
ecrireGraphe (fichier, graphe);
printf ("Le graphe a bien été écrit dans le fichier %s.\n", nomFichier);
close(fichier);
}
}
void ecrireGraphe (FILE* fichier, struct table* graphe) {
ecrireAretes (fichier, graphe->listeAretes, graphe->poid);
if (graphe->listeSuivante!=NULL) {
ecrireGraphe (fichier, graphe->listeSuivante);
}
}
void ecrireAretes (FILE* fichier, struct arete* aretes, int poid) {
fprintf (fichier, "\n%d %d %d", aretes->sommetDepart, aretes->sommetArrive, poid);
if (aretes->areteSuivante!=NULL) {
ecrireAretes (fichier, aretes->areteSuivante, poid);
}
} |
Partager