© Éditions Eyrolles
257
Correction des exercices
printf ("donnez le nom du fichier à créer : ");
gets (nomfich) ;
sortie = fopen (nomfich, "w") ;
printf (" --- pour finir la saisie, donnez un nom ‘vide' ---\n") ;
while ( printf ("nom
: "), gets (bloc.nom), strlen(bloc.nom) )
{ printf ("prénom
: ") ;
gets (bloc.prenom) ;
printf ("age
: ") ;
scanf ("%d", &bloc.age) ; getchar() ;
printf ("téléphone : ") ;
gets (bloc.tel) ;
fwrite (&bloc, sizeof(bloc), 1, sortie) ;
}
fclose (sortie) ;
}
Exercice 10.3
#include
#include
#define LGNOM
20
#define LGPRENOM 15
#define LGTEL
11
main()
{
char nomfich[21] ;
/* nom de fichier */
FILE * entree ;
struct { char nom [LGNOM+1] ;
char prenom [LGPRENOM+1] ;
int age ;
char tel [LGTEL+1] ;
} bloc ;
char nomcher [LGNOM+1] ;
/* nom recherché */
int trouve ;
/* indicateur nom trouvé */
printf ("donnez le nom du fichier à consulter : ");
gets (nomfich) ;
Delannoy Livre.book Page 257 Mercredi, 6. mai 2009 4:27 16
257
Correction des exercices
printf ("donnez le nom du fichier à créer : ");
gets (nomfich) ;
sortie = fopen (nomfich, "w") ;
printf (" --- pour finir la saisie, donnez un nom ‘vide' ---\n") ;
while ( printf ("nom
: "), gets (bloc.nom), strlen(bloc.nom) )
{ printf ("prénom
: ") ;
gets (bloc.prenom) ;
printf ("age
: ") ;
scanf ("%d", &bloc.age) ; getchar() ;
printf ("téléphone : ") ;
gets (bloc.tel) ;
fwrite (&bloc, sizeof(bloc), 1, sortie) ;
}
fclose (sortie) ;
}
Exercice 10.3
#include
#include
#define LGNOM
20
#define LGPRENOM 15
#define LGTEL
11
main()
{
char nomfich[21] ;
/* nom de fichier */
FILE * entree ;
struct { char nom [LGNOM+1] ;
char prenom [LGPRENOM+1] ;
int age ;
char tel [LGTEL+1] ;
} bloc ;
char nomcher [LGNOM+1] ;
/* nom recherché */
int trouve ;
/* indicateur nom trouvé */
printf ("donnez le nom du fichier à consulter : ");
gets (nomfich) ;
Delannoy Livre.book Page 257 Mercredi, 6. mai 2009 4:27 16
