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
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
void fclean(char *buffer, FILE *fp)
{
if (buffer != NULL && fp != NULL)
{
char *pc = strchr(buffer, '\n');
if (pc != NULL)
{
*pc = 0;
}
else
{
int c;
while ((c = fgetc(fp)) != '\n' && c != EOF)
{
}
}
}
}
/* -tc- main() renvoie toujours un entier de type int. */
int main(void)
{
/* Déclarations : */
FILE *p_fichier_sortant = NULL;
char nom_fichier_sortant[30] = "";
char porteur[16] = "";
char prenom[16];
char date[] = "AAMMJJ";
char heure[] = "HHMMSS";
char secu[16] = "";
int ret = EXIT_SUCCESS;
printf("Nom du fichier a creer : ");
fflush(stdout);
fgets(nom_fichier_sortant, sizeof nom_fichier_sortant, stdin);
fclean(nom_fichier_sortant, stdin);
printf("Nom du porteur : ");
fflush(stdout);
fgets(porteur, sizeof porteur, stdin);
fclean(porteur, stdin);
printf("Prenom du porteur : ");
fflush(stdout);
fgets(prenom, sizeof prenom, stdin);
fclean(prenom, stdin);
printf("Date (AAMMJJ) : ");
fflush(stdout);
fgets(date, sizeof date, stdin);
fclean(date, stdin);
printf("Heure (HHMMSS) : ");
fflush(stdout);
fgets(heure, sizeof heure, stdin);
fclean(heure, stdin);
printf("Secu : ");
fflush(stdout);
fgets(secu, sizeof secu, stdin);
fclean(secu, stdin);
p_fichier_sortant = fopen(nom_fichier_sortant, "w");
if (p_fichier_sortant != NULL)
{
fprintf(p_fichier_sortant,
"%s%s %s%s %s %s\n",
porteur, prenom, date, porteur, secu, prenom);
fclose(p_fichier_sortant), p_fichier_sortant = NULL;
}
else
{
/* -tc- Impossible d'ouvrir le fichier */
ret = EXIT_FAILURE;
}
return ret;
} |