Programmer en langage C
256
© Éditions Eyrolles
CHAPITRE 10
Exercice 10.1
#include
#define LGMAX 81
main()
{ char nomfich[21] ;
/* nom de fichier */
FILE * entree ;
int num = 1 ;
/* numéro de ligne */
char ligne [LGMAX] ;
/* tampon d’une ligne */
printf ("donnez le nom du fichier à lister : ");
scanf ("%20s", nomfich) ;
entree = fopen (nomfich, "r") ;
printf (" **** liste du fichier %s ****\n", nomfich) ;
while ( fgets (ligne, LGMAX, entree) )
{ printf ("%5d ", num++) ;
printf ("%s", ligne) ;
}
}
Certaines implémentations demanderont le mode « rt ».
Exercice 10.2
#include
#define LGNOM
20
#define LGPRENOM 15
#define LGTEL
11
main()
{
char nomfich[21] ;
/* nom de fichier */
FILE * sortie ;
struct { char nom [LGNOM+1] ;
char prenom [LGPRENOM+1] ;
int age ;
char tel [LGTEL+1] ;
} bloc ;
Delannoy Livre.book Page 256 Mercredi, 6. mai 2009 4:27 16
256
© Éditions Eyrolles
CHAPITRE 10
Exercice 10.1
#include
#define LGMAX 81
main()
{ char nomfich[21] ;
/* nom de fichier */
FILE * entree ;
int num = 1 ;
/* numéro de ligne */
char ligne [LGMAX] ;
/* tampon d’une ligne */
printf ("donnez le nom du fichier à lister : ");
scanf ("%20s", nomfich) ;
entree = fopen (nomfich, "r") ;
printf (" **** liste du fichier %s ****\n", nomfich) ;
while ( fgets (ligne, LGMAX, entree) )
{ printf ("%5d ", num++) ;
printf ("%s", ligne) ;
}
}
Certaines implémentations demanderont le mode « rt ».
Exercice 10.2
#include
#define LGNOM
20
#define LGPRENOM 15
#define LGTEL
11
main()
{
char nomfich[21] ;
/* nom de fichier */
FILE * sortie ;
struct { char nom [LGNOM+1] ;
char prenom [LGPRENOM+1] ;
int age ;
char tel [LGTEL+1] ;
} bloc ;
Delannoy Livre.book Page 256 Mercredi, 6. mai 2009 4:27 16
