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
|
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
/* int choix = 10; */
time_t date;
struct tm *timet;
char *stringdate;
char buffer[20];
char bufferAnniv[4];
char bufferNom[20];
int i = 4;
int c;
FILE *fp;
while ((c = getchar()) != '\n' && c != EOF); /* mange le tampon */
/* Ouverture du fichier en lecture et écriture */
fp = fopen ("anniversaire.txt", "rw");
printf("DEBUG \n");
if (fp == NULL)
{
perror("Fichier inexistant ! \n");
return EXIT_FAILURE;
}
printf("DEBUG \n");
stringdate = malloc (sizeof(char) * 10);
if (stringdate == NULL)
{
perror("Erreur allocation memoire chaine de caracteres ! \n");
return EXIT_FAILURE;
}
timet = malloc(sizeof(struct tm));
if (timet == NULL)
{
perror("Erreur allocation memoire structure time ! \n");
return EXIT_FAILURE;
}
/* Récupération de la date courante et on la formate comme ceci : jjmm */
time(&date);
timet = gmtime(&date);
strftime (stringdate, 10, "%d%m", timet);
printf(" --| Nous sommes le %s", stringdate);
/* Parcours du fichier */
/* dans bufferAnniv on recopie la date de l'entrée dans le fichier */
/* dans bufferNom on recopie le nom qui correspond a la date */
/* Si la date courante correspond a la date de l'entrée dans le fichier on affiche le nom de la personne */
while( fgets( buffer, sizeof(buffer), fp)!=NULL )
{
bufferAnniv[0] = buffer[0];
bufferAnniv[1] = buffer[1];
bufferAnniv[2] = buffer[2];
bufferAnniv[3] = buffer[3];
while(buffer[i] != '\n')
{
bufferNom[i - 4] = buffer[i];
i++;
}
if (strncmp(bufferAnniv, stringdate, 4) == 0)
{
printf(" --| C'est l'anniversaire de %s ! \n", bufferNom);
}
}
return EXIT_SUCCESS;
} |
Partager