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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "main.h"
#define CONTACT "contacts_.txt"
#define REPERTOIRE "repertoire.txt"
#define RDV "rendezvous.txt"
int lireContact(FILE* fichier, Contact* lecontact)
{
fichier = NULL;
char copy[100];
if (fichier == NULL)
{
fprintf(stderr,"ERROR : Can't open repertory file");
return 0;
}
else
{
;
while(fgets(copy,100,fichier)!= NULL)
{
lecontact = (Contact*) malloc (sizeof(Contact));
strcpy(lecontact->nom,copy);
// Prénom
fgets(copy,100,fichier);
strcpy(lecontact->prenom,copy);
// Num
fgets(copy,100,fichier);
strcpy(lecontact->num,copy);
// Mail
fgets(copy,100,fichier);
strcpy(lecontact->mail,copy);
// Adresse
fgets(copy,100,fichier);
strcpy(lecontact->adresse,copy);
// Ville
fgets(copy,100,fichier);
strcpy(lecontact->ville,copy);
}
printf("valeur %s",lecontact->ville);
}
fclose(fichier);
return 0;
}
// Fonction qui ajoute les contacts de contacts.txt au repertoire
int main(void){
// initialisation des pointeurs
FILE *f_contact = NULL;
FILE *f_repertoire = NULL;
char temp[100];
f_contact=fopen(CONTACT,"r");
f_repertoire = fopen(REPERTOIRE,"w");
if (f_contact == NULL){
fprintf(stderr,"ERROR : Can't open contact file");
return 0;
}
else{
// On génére le fichier binaire repertoire
// Ouverture, puis écriture
if((f_repertoire == NULL)){
fprintf(stderr,"ERROR : Can't create repertoire file");
fclose(f_contact);
return 0;
}
else{
int in_contact = 1;
char* ret;
ret = fgets(temp,100,f_contact);
// Tant qu'on est pas à la fin du fichier
while(ret != NULL)
{
// On test si on dans le champ contact
if(strcmp(temp,"==Contact==\r\n") == 0)
{
in_contact = 1;
// Tant qu'on est dans un même contact
while(in_contact == 1)
{
in_contact = 1;
// On lit une autre ligne
ret=fgets(temp,100,f_contact);
// Si on est sur un autre contact ou qu'on est à la fin, on sort
if(strcmp(temp,"==Contact==\r\n") == 0 || ret == NULL)
in_contact = 0;
// Sinon on enregistre la ligne si elle n'est pas vide
else if (strcmp(temp,"\n") != 0)
fputs(temp,f_repertoire);
}
}
else // ligne superflu avant ou entre contacts
ret=fgets(temp,100,f_contact);
}
}
}
printf("Copy success \n");
fclose(f_repertoire);
fclose(f_contact);
Contact lecontact;
FILE* fichier;
fichier = NULL;
fichier = fopen(REPERTOIRE,"r");
lireContact(fichier,&lecontact);
return 0;
} |
Partager