Here's my problem : I've a 2d char matrix wich I malloc with a function. Afterward, I want to get a map from a file, but I've got a segmentation fault right there and I dont know why... Here's a code sample :
// struct where I put the map and others informations from the
typedef struct problem_t
{
char *nom;
Coordonnees arrivee, depart;
int nb_ligne, nb_col;
char **
} Problem;
// Function wich malloc the map
int mallocCarte( char *** carte, int nbLigne, int nbCol )
{
*carte = malloc( nbLigne * sizeof( char* ) );
if ( *carte == NULL )
{
return false;
}
int i;
for ( i = 0; i < nbLigne ; ++i )
{
(*carte) [i] = malloc( nbCol * sizeof( char ) );
if ( (*carte) [i] == NULL )
{
return false;
}
}
return true;
} // mallocCarte ()
// Code sample, I've already got the others informations, now, I'd like to get the map
// On commence par reserver l'espace memoire reserve à la carte.
int res = mallocCarte( &problem->carte, problem->nb_ligne, problem->nb_col );
// Si l'allocation s'est mal passée, on renvoie un message
if ( res == false )
{
exc.num_Exc = MALLOC_ERROR;
exc.msg_Err = "Erreur lors de l'allocation mémoire de la carte";
return exc;
}
printf( "Recuperation de la carte 2 ...%d %d\n", problem->nb_ligne,
problem->nb_col );
int nbLi = 0;
int nbCol = 0;
while ( fgets( fromFile, 1, file ) != NULL && nbLi < problem->nb_ligne )
{
if ( fromFile [0] == '\n' )
{
nbCol = 0;
++nbLi;
continue;
}
if ( nbCol == problem->nb_col )
{
printf( "malformed input file!\n" );
exit( -1 );
}
( problem->carte ) [nbLi] [nbCol++] = fromFile [0];
}
It's been many days and I really don't know what to do... I'd be so greatful If someone could help me !
Thanks you
(Here is the source file where I take informations. First they are problem name, then some coordinates, and finally the map size. At the end of the file is the map https://dl.dropbox.com/u/56951442/map.txt )