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
| #include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<cctype>
int TAILLE_MAX=0;
typedef struct profile profile;
struct profile
{
int id;
char nom[20];
char prenom[30];
char numtel[11];
char email[20];
};
int realloc_tab(profile *tab)
{
profile *temp = NULL;//on définit ici un pointeur temp sur la structure profile
temp = (profile *) realloc (tab,TAILLE_MAX * sizeof(profile));
if ( temp == NULL )
{
fprintf(stderr,"Reallocation impossible");
free(tab);
exit(EXIT_FAILURE);
return 0;
}
else {
tab = temp;
return 1;
}
}
//On ajoute ici le nouvel utilisateur dans le tableau et dans le fichier
int create_user(char nom[20], char prenom[30], char numtel[11], char email[30], profile *tab)
{
char ch[30];
int i=0;
i=TAILLE_MAX;//on définit la case d'ajout ici elle correspond à la TAILLE_MAX puisque le tableau commence à 0
TAILLE_MAX++;//on incrémente la taille du tableau
if(realloc_tab(tab)==1){
tab[i].id=i+1;
strcpy(tab[i].nom,strlwr(nom));
strcpy(tab[i].prenom,strlwr(prenom));
strcpy(tab[i].numtel,numtel);
strcpy(tab[i].email,strlwr(email));
return 1; } else { return 0;}
}
int main(void) {
FILE *pf;
char chaine[100],nom[20]="durand",prenom[30]="ererer",numtel[11]="0241361227",email[30]="dfdfdf",rep;
int i=0,choix;
profile * tab = NULL;
pf = fopen("C:\\Annuaire\\test.txt","r");
do
{
if (fgetc(pf)=='\n')
{
TAILLE_MAX++;
}
}while(!feof(pf));
rewind(pf);
tab = (profile *) malloc (TAILLE_MAX * sizeof(profile));
if( tab == NULL )
{
fprintf(stderr,"Allocation impossible");
exit(EXIT_FAILURE);
}
if (pf != NULL)
{
do
{
fgets(chaine,100,pf);
sscanf(chaine,"| %d | %s | %s | %s | %s |",&tab[i].id,tab[i].nom,tab[i].prenom,tab[i].numtel,tab[i].email);
i++;
}while(i!=TAILLE_MAX);
}
fclose(pf);
do
{
system("cls");
for(i=0;i<TAILLE_MAX;i++) printf("| %d | %s | %s | %s | %s |\n",tab[i].id,tab[i].nom,tab[i].prenom,tab[i].numtel,tab[i].email);
printf("\nQue voulez-vous faire ? :\n\n1-Ajouter un utilisateur\n....");
printf("\n\nVotre choix : ");
scanf("%d",&choix);
printf("\n");
switch(choix) {
case 1: {
create_user(nom,prenom,numtel,email,tab);
}
break;
case 2: {
}
break;
case 3: {
}
break;
default: {printf("\nErreur de saisie !!!\n\n");
getch(); }
}
}while(rep!='n' && rep!='N');
getch();
return 0;
} |
Partager