Problème comparaison de chaines
Bonsoir, peut-être bon appétit ? ;)
Voila j'ai un problème sur ce code (j'obtiens une erreur de segmentation) je pense que le problème vient du strcmp dans le while (i<G.n || strcmp(G.tabsommet[i].Ip,Ip1))!=0) de la fonction chargement .
J'avais fait quelques tests précédemment en m'arrangeant pour que les deux chaines soient égales et le strcmp devait renvoyer 0 ... mais non ^^ .
Le but est de créer une matrice représentant les differentes liaisons dans un réseau, afin de créer par la suite un graphe.
Pour cela on dispose d'un fichier dont la déscription est donnée au début du code.
D'avance Merci :D
Code:
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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Format de fichier Imaginé
/*
nb //Nombre de sommets
//Liste des Sommets Associés a leur Ip
Sommet1 0.0.0.1
Sommet2 0.0.0.2
.
.
.
Sommetnb *.*.*.*
//Liste des Connexions
0.0.0.1 0.0.0.2
0.0.0.1 0.0.0.3
.
.
.
*.*.*.* *.*.*.*
EOF
*/
typedef struct {
char nom[50];
char Ip[16];
}TypeSommet;
typedef struct{
TypeSommet * tabsommet;
char ** matrice;
int n;
}Graphe;
char ** Alloc (int n)
{
int i;
char ** mat;
mat=(char**)malloc(n*sizeof(char*));
for(i=0;i<n;i++)
mat[i]=(char*)calloc(n,sizeof(char));
return mat;
}
Graphe Chargement (char * fichier)
{
Graphe G;
int i,j;
char Ip1[16],Ip2[16];
FILE *fp;
if((fp=fopen(fichier,"rt"))==NULL)
{
puts("Erreur d'ouverture du fichier");
exit(1);
}
fscanf(fp,"%d",&G.n);
G.matrice=Alloc(G.n);
G.tabsommet=(TypeSommet*)malloc(G.n*sizeof(TypeSommet));
for(i=0;i<G.n;i++)
{
fscanf(fp,"%s",G.tabsommet[i].nom);
fscanf(fp,"%s",G.tabsommet[i].Ip);
}
while (!feof(fp))
{
fscanf(fp,"%s",Ip1);
fscanf(fp,"%s",Ip2);
i= 0;
while (i<G.n || strcmp(G.tabsommet[i].Ip,Ip1))!=0) i++;
printf("%d\n",i);
j=0;
while (j<G.n || strcmp(Ip2,G.tabsommet[j].Ip)!=0) j++;
printf("%d",j);
G.matrice[i][j]=1;
}
fclose(fp);
return G;
}
void Affiche (Graphe G)
{
int i,j;
for(i=0;i<G.n;i++)
{
for (j=0;j<G.n;j++)printf("%c",G.matrice[i][j]);
puts("");
}
}
int main ()
{
Graphe G;
G=Chargement("Sauvegarde.txt");
Affiche(G);
return 0;
} |