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 90 91 92
|
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void fclean(char *, FILE *);
int main (void)
{
int nombrefiches=0;
int end = 0;
char (*p_tab)[50] = NULL;
int err = EXIT_SUCCESS;
printf("Programme de gestion de Fiches Personnes :\n");
printf("Combien de Fiches personnes souhaitez-vous enregistrer ?\n");
scanf("%i",&nombrefiches);
printf("Vous voulez enregistrer %i Fiches Personnes\n",nombrefiches);
system("pause");
p_tab = malloc(nombrefiches * sizeof *p_tab);
if (p_tab != NULL)
{
int i=1;
/* Initialisation des n premiers octets du tableau p_tab avec le caractère 0*/
memset(p_tab, '0', nombrefiches * sizeof (*p_tab));
/* Saisie des chaines de caractère */
for (i = 1; i < nombrefiches+1 && end == 0; i++)
{
printf("Entrez la Fiche Personne n°%i:",i);
fflush(stdout); //vidage du tampon après chaque saisie
if (fgets(p_tab[i], sizeof p_tab[i], stdin) != NULL)
{
fclean(p_tab[i], stdin);
if (strcmp(p_tab[i], "quit") == 0)
{
*p_tab[i] = 0;
end = 1;
}
}
}
/* Affichage des chaines saisies */
for (i = 1; i < nombrefiches+1 && *p_tab[i] != 0; ++i)
{
puts(p_tab[i]);
}
free(p_tab);
p_tab = NULL;
}
else
{
fprintf(stderr, "Erreur d'allocation mémoire!\n");
system("pause");
err = EXIT_FAILURE;
}
system("pause");
printf("\n");
return err;
}
void fclean(char *s_buff, FILE *fp)
{
char *pc = strchr(s_buff, '\n');
if (pc != NULL)
{
*pc = 0;
}
else
{
int c;
while ((c = fgetc(fp)) != '\n' && c != EOF)
{
continue;
}
}
} |
Partager