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
| #include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
typedef struct
{
char nom[20];
char adresse[20];
int nbpassage; /* pas encore payé */
int num;
}
abonne;
static void fclean (char *s, FILE * fp)
{
char *p = strchr (s, '\n');
if (p != NULL)
{
*p = 0;
}
else
{
int c;
while ((c = getc (fp)) != '\n' && c != EOF)
{
}
}
}
void creation (FILE * f)
{
int i = 1;
char s[16];
do
{
abonne x = { 0 };
printf ("donner le nom de %d eme abonnee :\n", i);
fgets (x.nom, sizeof x.nom, stdin);
fclean (x.nom, stdin);
printf ("donner l'adresse de l'abonnee:\n");
fgets (x.adresse, sizeof x.adresse, stdin);
fclean (x.adresse, stdin);
printf ("donner le nombre de passage non paye de l'abonnee:\n");
fgets (s, sizeof s, stdin);
fclean (s, stdin);
x.nbpassage = (int) strtol (s, NULL, 10);
printf ("donner le numero de l'abonne dans le fichier \n");
fgets (s, sizeof s, stdin);
fclean (s, stdin);
x.num = (int) strtol (s, NULL, 10);
fwrite (&x, sizeof (abonne), 1, f);
i++;
printf ("voulez vous saisir un autre abonne o/n:\n");
fgets (s, sizeof s, stdin);
fclean (s, stdin);
}
while (tolower (s[0]) == 'o');
fclose (f);
}
int main (void)
{
int ret = EXIT_SUCCESS;
FILE *fichier = fopen ("fichier.txt", "ab");
if (fichier == NULL)
{
printf ("fichier inexistant !");
ret = EXIT_FAILURE;
}
else
{
creation (fichier);
fclose (fichier), fichier = NULL;
}
return ret;
} |
Partager