Bonjour,
Je ne comprends pas pourquoi l'ouverture d'un flot perturbe l'affichage de ma "fiche".
Un résumé avant le code complet :
Fiche est une structure composée de 3 char[] (nom,prenom,tel).
Je crée une Fiche fiche1, je demande de l'afficher. Aucun problème.
Puis j'ouvre un flot. Et sans rien faire de plus (en tout cas rien de volontaire). Quand je fais afficher ma fiche, le prenom a ... disparu !!

Merci d'avance.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#define TNOM 64
#define TTEL 10
#define MAXFICHES 10
 
typedef struct Fiche *Fiche;
struct Fiche {
    char nom[TNOM];
    char prenom[TNOM];
    char tel[TTEL];
};
 
typedef Fiche AnnuaireT[MAXFICHES];
 
Fiche newFiche(char * nom, char * prenom, char *tel)
{
  Fiche fiche=(Fiche) calloc(1,sizeof(Fiche));
  if (fiche==NULL)
  {
  	printf("Pb Calloc");
  	exit(0);
  }
  strcpy(fiche->nom,nom);
	strcpy(fiche->prenom,prenom);
	strcpy(fiche->tel,tel);
 
  return fiche;
}
 
 
char * lireLigne(char *dst,size_t taille,FILE *flot){
  int i;
  if(fgets(dst,taille,flot)==NULL)
    {printf("Dans lireLigne \n");return NULL;}
  /* Enlever l'éventuel \n */
  i = strlen(dst)-1;
  if (i <= 0)
    {printf("Un seulchar \n");return NULL;}
  if( dst[i] == '\n')
    dst[i] = '\0';
  return dst;
}
 
Fiche lireFiche(FILE * flot)
{
  char nom[TNOM], prenom[TNOM],tel[TTEL];
  Fiche fiche;
 
  if (lireLigne(nom,TNOM,flot) == NULL) /* fin atteinte */
    return NULL;
 
  lireLigne(prenom,TNOM,flot);
 
  lireLigne(tel,TTEL+1,flot);
  fiche = newFiche(nom,prenom,tel);
  return fiche;
}
 
int lireTout(const char * source, AnnuaireT cible)
{
  FILE * flot;
  int i=0;
 
  flot = fopen(source,"r"); /* ouverture en lecture */
  if (flot == NULL) {
    perror("erreur ouverture fichier");
    return 0;
  }
 
  do {
    cible[i] = lireFiche(flot);
    printf("coucoup %d vrai ? %d\n",i,(cible[i]==NULL));
    i++;
  }
  while ( (i<MAXFICHES) && (cible[i-1] != NULL) );
 
  fclose(flot);
 
  if (cible[i-1] == NULL) {
      return i-1;
  } else {
    return MAXFICHES;
  }
}
 
void afFiche(Fiche f)
{
  printf("%s\n%s\n%10s\n",f->nom,f->prenom,f->tel);
}
 
void afficheAnnuaireT(AnnuaireT a)
{
  int i=0;
 
  while ( (i < MAXFICHES) && (a[i] != NULL) )
    {
      afFiche(a[i]);
      i++;
    }   
  printf("\n");
}
 
int sauverFiche(Fiche fiche, FILE * flot)
{
  int ret=0;
 
  if (fiche != NULL)
    ret=fprintf(flot,"%s\n%s\n%10s\n",
                fiche->nom,
                fiche->prenom,
                fiche->tel);
  return ret;
}
 
int main(void)
{
	int i;
	AnnuaireT myAnnuaire;
	Fiche fiche1=newFiche("AUBRAC","Lucie","1111111111");
	FILE *perso;
 
	for(i=0;i<MAXFICHES;i++)
		myAnnuaire[i]=NULL;
	myAnnuaire[0]=fiche1;
 
	/*perso=fopen("Test1.txt","w");
	if (perso == NULL) {
    perror("erreur ouverture fichier");
    return 0;
  }//*/
 
	afficheAnnuaireT(myAnnuaire);
 
	//fclose(perso);
 
	return EXIT_FAILURE;
}