Programmer en langage C
258
© Éditions Eyrolles
entree = fopen (nomfich, "r") ;
printf (" quel nom recherchez-vous : ") ;
gets (nomcher) ;
trouve = 0 ;
do
{ fread (&bloc, sizeof(bloc), 1, entree) ;
if ( strcmp (nomcher, bloc.nom)==0 ) trouve = 1 ;
}
while ( (!trouve) && (!feof(entree)) ) ;
if (trouve)
{ printf ("prénom
: %s\n", bloc.prenom) ;
printf ("âge
: %d\n", bloc.age) ;
printf ("téléphone : %s\n", bloc.tel) ;
}
else printf ("-- ce nom ne figure pas au fichier --") ;
}
Exercice 10.4
#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 ;
int num ;
/* numéro de bloc cherché */
long taille,
/* taille du fichier en octets */
pos ;
/* position dans le fichier */
printf ("donnez le nom du fichier à consulter : ");
gets (nomfich) ;
Delannoy Livre.book Page 258 Mercredi, 6. mai 2009 4:27 16
258
© Éditions Eyrolles
entree = fopen (nomfich, "r") ;
printf (" quel nom recherchez-vous : ") ;
gets (nomcher) ;
trouve = 0 ;
do
{ fread (&bloc, sizeof(bloc), 1, entree) ;
if ( strcmp (nomcher, bloc.nom)==0 ) trouve = 1 ;
}
while ( (!trouve) && (!feof(entree)) ) ;
if (trouve)
{ printf ("prénom
: %s\n", bloc.prenom) ;
printf ("âge
: %d\n", bloc.age) ;
printf ("téléphone : %s\n", bloc.tel) ;
}
else printf ("-- ce nom ne figure pas au fichier --") ;
}
Exercice 10.4
#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 ;
int num ;
/* numéro de bloc cherché */
long taille,
/* taille du fichier en octets */
pos ;
/* position dans le fichier */
printf ("donnez le nom du fichier à consulter : ");
gets (nomfich) ;
Delannoy Livre.book Page 258 Mercredi, 6. mai 2009 4:27 16
