[c] pour les motivés ,structurer un fichier

cs_Diony Messages postés 11 Date d'inscription mercredi 28 avril 2004 Statut Membre Dernière intervention 27 mars 2006 - 14 mai 2004 à 18:03
cs_Diony Messages postés 11 Date d'inscription mercredi 28 avril 2004 Statut Membre Dernière intervention 27 mars 2006 - 14 mai 2004 à 23:19
bonjour , voici mon programme . je n arrive pas a structurer mon fichier a cause de la fonction inserer_recette_fin_liste qui doit normalement inserer un maillon recette a la fin de la liste mais voila ca plante, j ai mis une pause entre chaque étape, pour voir d ou venait le probleme , et a priori ca plante après le malloc compris dans la fonction.je commence a etre bien perdu, pouvez m aider .si vous voyez des choses incohérentes par hasard , pouvez vous me les signaler.merci d avance.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct recette { char * nom;
struct ingredient * liste_ing;
char * tps;
char * facon;
struct recette * rec_suiv;
}recette;

typedef struct ingredient {char * nom_ing;
char * quantite;
struct ingredient * ing_suiv;
}ingredient;

typedef struct recette * liste_recettes;

typedef struct ingredient * liste_ing;

/*void creer_ingredient(char p
else{i.nom_ing=NULL;
i.quantite=NULL;
i.ing_suiv=NULL;}
}*/

void creer_ingredient(char t0[1000], liste_ing ing){

if (t0[1000]!=':'){
ing=(liste_ing)malloc(sizeof(ingredient));
ing->nom_ing=strtok(NULL,",");
printf("%s\n",ing->nom_ing);
ing->quantite=strtok(NULL,";");
printf("%s\n",ing->quantite);
system ("PAUSE");

creer_ingredient(t0,ing->ing_suiv);
}
else {printf("nothing");
}
}

void inserer_recette_fin_liste(char a[1000],liste_recettes *l) {
char t0[1000];
strcpy(t0,a);
if (l==NULL) {

printf("%s \n",t0);
system("PAUSE");

(*l)=(liste_recettes )malloc(sizeof(recette));

(*l)->nom=strtok(t0,":");//renvoie le string situé avant le premier :
printf("%s\n",(*l)->nom);
system("PAUSE");

creer_ingredient(t0,(*l)->liste_ing);
system("PAUSE");

(*l)->tps=strtok(NULL,":");
printf("%s \n",(*l)->tps);
system("PAUSE");

(*l)->facon=strtok(strrchr(t0,':'),"\n");
printf("%s \n",(*l)->facon);
system("PAUSE");

(*l)->rec_suiv=NULL;
system("PAUSE");
}


else inserer_recette_fin_liste(t0,&((*l)->rec_suiv));
}

void effacer_tab(char t[1000]){
int i;
for (i=0;i<strlen(t);i++) {
t[i]=' ';}
t[0]='\0';}

void structure (liste_recettes *l) {
FILE *r;
char a[1000],c;
int i=0;
r= fopen ("recettes.dat", "r");
if (!r) {printf ("*** Impossible d'ouvrir le fichier ***\n");
system("PAUSE");
//menu();
}
else {while(!feof(r)) {

do { c=fgetc(r);
a[i]=c;
i++;} while (c!='\n');
printf("%s \n\n",a);
system("PAUSE");
inserer_recette_fin_liste(a,l);
i=0;
effacer_tab(a);
}}
}

int main()
{
liste_recettes *l;
l=NULL;
structure(l);
}

4 réponses

cs_djl Messages postés 3011 Date d'inscription jeudi 26 septembre 2002 Statut Membre Dernière intervention 27 novembre 2004 7
14 mai 2004 à 19:22
dans ta fonction tu fait au debut char t0[1000]; c'est tres dangereux, fait plutot

char *t0;
t0=malloc(1000);
0
cs_Diony Messages postés 11 Date d'inscription mercredi 28 avril 2004 Statut Membre Dernière intervention 27 mars 2006
14 mai 2004 à 21:32
merci djl c sympa , mais pour linstant ca résoud pas beaucoup mon problème.je le changerai après.le to[1000] est pas définitif.
0
cs_djl Messages postés 3011 Date d'inscription jeudi 26 septembre 2002 Statut Membre Dernière intervention 27 novembre 2004 7
14 mai 2004 à 21:52
ben le probleme est simple, tu alloue pas les buffer de tes structures

accorde toi quelque facilité d'ecriture

void effacer_tab(char t[])

if (!r) {printf ("*** Impossible d'ouvrir le fichier ***\n");
system("PAUSE");
return;
}

malloc(sizeof ingredient);
0
cs_Diony Messages postés 11 Date d'inscription mercredi 28 avril 2004 Statut Membre Dernière intervention 27 mars 2006
14 mai 2004 à 23:19
ok djl
c bon , en refaisant toute ma fonction g resolu le pb.
merci kan meme a toi.t le seul a m avoir répondu.
0
Rejoignez-nous