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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void clean (char *s_buffer)
{
char *nl = strchr (s_buffer, '\n');
if (nl != NULL)
{
*nl = 0;
}
else
{
int c;
while ((c = getchar ()) != '\n' && c != EOF)
{
}
}
}
int main (void)
{
char nomfich[21];
printf ("Donnez un nom a votre fichier : ");
fgets (nomfich, sizeof nomfich, stdin);
clean (nomfich);
FILE *sortie = fopen (nomfich, "wb");
if (sortie != NULL)
{
while (1)
{
struct
{
char nom[21];
char prenom[16];
int age;
char tel[11];
}
personne;
printf ("\nNom ( <enter> pour quitter) : ");
fgets (personne.nom, sizeof personne.nom, stdin);
clean (personne.nom);
if (*personne.nom == 0)
{
break;
}
printf ("Prenom : ");
fgets (personne.prenom, sizeof personne.prenom, stdin);
clean (personne.prenom);
printf ("Age : ");
{
char s[4];
fgets (s, sizeof s, stdin);
clean (s);
personne.age = (int) strtol (s, NULL, 10);
}
printf ("Numero de telephone : ");
fgets (personne.tel, sizeof personne.tel, stdin);
clean (personne.tel);
if ((fwrite (&personne, sizeof personne, 1, sortie)) != 1)
{
printf ("--- Erreur d'ecriture ---");
}
}
fclose (sortie);
}
return 0;
} |
Partager