Rechercher : dans
Par :

[Langage C] Problème supprimer contact carnet

Dernière réponse le 12 nov 2008 à 16:17:35 nicochamadelol, le 8 nov 2008 à 17:42:31 
 Signaler ce message aux modérateurs

Bonjour à tous


J'ai un petit problème!
Je doit faire un programme en C qui fait un carnet d'adresse.
dans le cas numéro 3 je doit supprimer un contact qui est dans un fichier texte.
mais cela ne fonctionne pas.
pouvez vous m'aider pour trouver mes erreur.
Merci d'avance!

voila le code source et ce qui s'affiche dans mon terminal:

#include <stdio.h>
#include <stdlib.h>

#include <string.h>


typedef struct Personne
{
char nom[50];
char prenom[50];
char telephone[16];
char age[3];
}Personne;



int main()

{
int i=0, nbLignes=0, menu = 0, supnom;
Personne contact[50];
Personne contactSup;

FILE* fichier = NULL;
FILE* fichier1 = NULL;
FILE* fich = NULL;
FILE* fichsup = NULL;
fichier = fopen("adresse.txt", "r+");

if (fichier != NULL)
{
while(fscanf(fichier, "%s %s %s %s\n", contact[nbLignes].nom, contact[nbLignes].prenom, contact[nbLignes].telephone, contact[nbLignes].age) == 4)
nbLignes++;
fclose(fichier);
}

printf("\n\t\t\tC A R N E T D ' A D R E S S E\n\n\n" );


printf("1. Ajouter un contact\n" );

printf("2. Afficher tout les contacts\n" );
printf("3. Supprimer un contact\n");

printf("\n" );


printf("Votre choix : " );

scanf("%ld", &menu);




switch (menu)

{

case 1 : //ajouter contact
fichier1 = fopen("adresse.txt", "r+");

if ((fichier1 != NULL) && (nbLignes < 50))
{
while(fscanf(fichier1, "%s %s %s %s\n", contact[nbLignes].nom, contact[nbLignes].prenom, contact[nbLignes].telephone, contact[nbLignes].age) == 4)
nbLignes++;

if(nbLignes==0)
fseek(fichier1, -1, SEEK_CUR);

printf("\nNom : ");
scanf("%s", &contact[nbLignes].nom);
fprintf(fichier1, "%s ", contact[nbLignes].nom);

printf("Prenom : ");
scanf("%s", &contact[nbLignes].prenom);
fprintf(fichier1, "%s ", contact[nbLignes].prenom);

printf("Telephone : ");
scanf("%s", &contact[nbLignes].telephone);
fprintf(fichier1, "%s ", contact[nbLignes].telephone);

printf("Age : ");
scanf("%s", &contact[nbLignes].age);
fprintf(fichier1, "%s\n", contact[nbLignes].age);
fclose(fichier1);
}
else
{
printf("\nLe carnet d'adresse est plein !!\n\n");
}
break;



case 2 : //afficher contact
fich = fopen("adresse.txt", "r+");

if (fich != NULL)
{
nbLignes=0;
while(fscanf(fich, "%s %s %s %s\n", contact[nbLignes].nom, contact[nbLignes].prenom, contact[nbLignes].telephone, contact[nbLignes].age) == 4)
nbLignes++;

for(i=0;i<nbLignes;i++)
{
printf("\nNom: %s\nPrenom: %s\nTelephone: %s\nAge: %s\n\n", contact[i].nom, contact[i].prenom, contact[i].telephone, contact[i].age);
}

if( nbLignes == 0)
{
printf("Il n'y a pas de contact dans le carnet d'adresse !!!\n\n");
}
fclose(fich);
}

break;

case 3 : //supprimer un contact

fichsup = fopen("adresse.txt", "r+");
int i = 0, j = 0;

printf ("Nom du contact a supprimer ?\n ");
scanf ("%s", supnom);

while(i + j < nbLignes)
{
fscanf(fichsup, "%s %s %s %s\n", contactSup.nom, contactSup.prenom, contactSup.telephone, contactSup.age);
if(strcmp(supnom, contactSup.nom) != 0)
{
strcpy(contact[i].nom, contactSup.nom);
strcpy(contact[i].prenom, contactSup.prenom);
strcpy(contact[i].telephone, contactSup.telephone);
strcpy(contact[i].age, contactSup.age);
i++;
}
else
j++;
}

default :

printf("\nMauvaise entree\n\n" );

break;

}



return 0;

}

Configuration: Windows XP
Firefox 3.0.3

Meilleures réponses pour « [Langage C] Problème supprimer contact carnet » dans :
Langage C - Les types de données Voir Les types de données Les données manipulées en langage C sont typées, c'est-à-dire que pour chaque donnée que l'on utilise (dans les variables par exemple) il faut préciser le type de donnée, ce qui permet de connaître l'occupation mémoire (le...
Langage C++ - Les types de données VoirLes types de données Les données manipulées en langage C++, comme en langage C, sont typées, c'est-à-dire que pour chaque donnée que l'on utilise (dans les variables par exemple) il faut préciser le type de donnée, ce qui permet de connaître...
Les chaînes de caractères en C++ VoirQu'est-ce qu'une chaîne de caractères ? Une chaîne de caractères (appelée string en anglais) est une suite de caractères, c'est-à-dire un ensemble de symboles faisant partie du jeu de caractères, défini par le code ASCII. En langage C++, une...
Langage C - Les chaînes de caractères VoirQu'est-ce qu'une chaîne de caractères ? Une chaîne de caractères (appelée string en anglais) est une suite de caractères, c'est-à-dire un ensemble de symboles faisant partie du jeu de caractères, défini par le code ASCII. En langage C, une chaîne...

1

nicochamadelol, le 8 nov 2008 à 20:46:05

Bonsoir,

Merci de vos réponses

Cordialement

Répondre à nicochamadelol

2

nicochamadelol, le 11 nov 2008 à 16:58:27

Bonjour,

Je viens de changer le code mais ca ne fonctionne pas encore.

Une petite aide généreuse???

#include <stdio.h>
#include <stdlib.h>

#include <string.h>


typedef struct Personne
{
char nom[50];
char prenom[50];
char telephone[16];
char age[3];
}Personne;



int main()

{
int i=0, nbLignes=0, menu = 0, supnom;
Personne contact[50];
Personne contactSup;

FILE* fichier = NULL;
FILE* fichier1 = NULL;
FILE* fich = NULL;
FILE* fichsup = NULL;
fichier = fopen("adresse.txt", "r+");

if (fichier != NULL)
{
while(fscanf(fichier, "%s %s %s %s\n", contact[nbLignes].nom, contact[nbLignes].prenom, contact[nbLignes].telephone, contact[nbLignes].age) == 4)
nbLignes++;
fclose(fichier);
}

printf("\n\t\t\tC A R N E T D ' A D R E S S E\n\n\n" );


printf("1. Ajouter un contact\n" );

printf("2. Afficher tout les contacts\n" );
printf("3. Supprimer un contact\n");

printf("\n" );


printf("Votre choix : " );

scanf("%ld", &menu);




switch (menu)

{

case 1 : //ajouter contact
fichier1 = fopen("adresse.txt", "r+");

if ((fichier1 != NULL) && (nbLignes < 50))
{
while(fscanf(fichier1, "%s %s %s %s\n", contact[nbLignes].nom, contact[nbLignes].prenom, contact[nbLignes].telephone, contact[nbLignes].age) == 4)
nbLignes++;

if(nbLignes==0)
fseek(fichier1, -1, SEEK_CUR);

printf("\nNom : ");
scanf("%s", &contact[nbLignes].nom);
fprintf(fichier1, "%s ", contact[nbLignes].nom);

printf("Prenom : ");
scanf("%s", &contact[nbLignes].prenom);
fprintf(fichier1, "%s ", contact[nbLignes].prenom);

printf("Telephone : ");
scanf("%s", &contact[nbLignes].telephone);
fprintf(fichier1, "%s ", contact[nbLignes].telephone);

printf("Age : ");
scanf("%s", &contact[nbLignes].age);
fprintf(fichier1, "%s\n", contact[nbLignes].age);
fclose(fichier1);
}
else
{
printf("\nLe carnet d'adresse est plein !!\n\n");
}
break;



case 2 : //afficher contact
fich = fopen("adresse.txt", "r+");

if (fich != NULL)
{
nbLignes=0;
while(fscanf(fich, "%s %s %s %s\n", contact[nbLignes].nom, contact[nbLignes].prenom, contact[nbLignes].telephone, contact[nbLignes].age) == 4)
nbLignes++;

for(i=0;i<nbLignes;i++)
{
printf("\nNom: %s\nPrenom: %s\nTelephone: %s\nAge: %s\n\n", contact[i].nom, contact[i].prenom, contact[i].telephone, contact[i].age);
}

if( nbLignes == 0)
{
printf("Il n'y a pas de contact dans le carnet d'adresse !!!\n\n");
}
fclose(fich);
}

break;

case 3 : //supprimer un contact

fichsup = fopen("adresse.txt", "r+");
int i = 0, j = 0;
char supnom[50];

printf ("Nom du contact a supprimer ?\n ");
scanf ("%s", supnom);

while(i + j < nbLignes)
{
fscanf(fichsup, "%s %s %s %s\n", contactSup.nom, contactSup.prenom, contactSup.telephone, contactSup.age);
if(strcmp(supnom, contactSup.nom) != 0)
{
strcpy(contact[i].nom, contactSup.nom);
strcpy(contact[i].prenom, contactSup.prenom);
strcpy(contact[i].telephone, contactSup.telephone);
strcpy(contact[i].age, contactSup.age);
i++;
}
else
j++;
}

default :

printf("\nMauvaise entree\n\n" );

break;

}



return 0;

}



Dans la console j'ai ceci :

nicolas@nicolas-desktop:~$ cc -o karnet karnet.c
nicolas@nicolas-desktop:~$ ./karnet

C A R N E T D ' A D R E S S E


1. Ajouter un contact
2. Afficher tout les contacts
3. Supprimer un contact

Votre choix : 2

Nom: Nom1
Prenom: prenom1
Telephone: tel1
Age: 20


Nom: Nom2
Prenom: prenom2
Telephone: tel2
Age: 21

nicolas@nicolas-desktop:~$ ./karnet

C A R N E T D ' A D R E S S E


1. Ajouter un contact
2. Afficher tout les contacts
3. Supprimer un contact

Votre choix : 3
Nom du contact a supprimer ?
Nom1

Mauvaise entree

nicolas@nicolas-desktop:~$ ./karnet

C A R N E T D ' A D R E S S E


1. Ajouter un contact
2. Afficher tout les contacts
3. Supprimer un contact

Votre choix : 2

Nom: Nom1
Prenom: prenom1
Telephone: tel1
Age: 20


Nom: Nom2
Prenom: prenom2
Telephone: tel2
Age: 21

nicolas@nicolas-desktop:~$

Répondre à nicochamadelol

3

mype, le 11 nov 2008 à 18:14:30

Il faudrait que tu utilise un fichier tapon pour supprimer un contact...
tu ecris dedans les contact non supprimer puis tu recopie le fichier tampon dans adresse
essaye ça

#include <stdio.h>
#include <stdlib.h>

#include <string.h>


typedef struct Personne
{
char nom[50];
char prenom[50];
char telephone[16];
char age[3];
}Personne;



int main()

{
int i=0, nbLignes=0, menu = 0;
Personne contact[50];
Personne contactSup;

FILE* fichier = NULL;
FILE* fichier1 = NULL;
FILE* fich = NULL;
FILE* fichsup = NULL;
fichier = fopen("adresse.txt", "r+");

if (fichier != NULL)
{
while(fscanf(fichier, "%s %s %s %s\n", contact[nbLignes].nom, contact[nbLignes].prenom, contact[nbLignes].telephone, contact[nbLignes].age) == 4)
nbLignes++;
fclose(fichier);
}

printf("\n\t\t\tC A R N E T D ' A D R E S S E\n\n\n" );


printf("1. Ajouter un contact\n" );

printf("2. Afficher tout les contacts\n" );
printf("3. Supprimer un contact\n");

printf("\n" );


printf("Votre choix : " );

scanf("%ld", &menu);




switch (menu)

{

case 1 : //ajouter contact
fichier1 = fopen("adresse.txt", "r+");

if ((fichier1 != NULL) && (nbLignes < 50))
{
while(fscanf(fichier1, "%s %s %s %s\n", contact[nbLignes].nom, contact[nbLignes].prenom, contact[nbLignes].telephone, contact[nbLignes].age) == 4)
nbLignes++;

if(nbLignes==0)
fseek(fichier1, -1, SEEK_CUR);

printf("\nNom : ");
scanf("%s", &contact[nbLignes].nom);
fprintf(fichier1, "%s ", contact[nbLignes].nom);

printf("Prenom : ");
scanf("%s", &contact[nbLignes].prenom);
fprintf(fichier1, "%s ", contact[nbLignes].prenom);

printf("Telephone : ");
scanf("%s", &contact[nbLignes].telephone);
fprintf(fichier1, "%s ", contact[nbLignes].telephone);

printf("Age : ");
scanf("%s", &contact[nbLignes].age);
fprintf(fichier1, "%s\n", contact[nbLignes].age);
fclose(fichier1);
}
else
{
printf("\nLe carnet d'adresse est plein !!\n\n");
}
break;



case 2 : //afficher contact
fich = fopen("adresse.txt", "r+");

if (fich != NULL)
{
nbLignes=0;
while(fscanf(fich, "%s %s %s %s\n", contact[nbLignes].nom, contact[nbLignes].prenom, contact[nbLignes].telephone, contact[nbLignes].age) == 4)
nbLignes++;

for(i=0;i<nbLignes;i++)
{
printf("\nNom: %s\nPrenom: %s\nTelephone: %s\nAge: %s\n\n", contact[i].nom, contact[i].prenom, contact[i].telephone, contact[i].age);
}

if( nbLignes == 0)
{
printf("Il n'y a pas de contact dans le carnet d'adresse !!!\n\n");
}
fclose(fich);
}

break;

case 3 : {//supprimer un contact

fichsup = fopen("adresse.txt", "r+");
fichier=fopen("tampon.txt","w");
int i = 0, j = 0;
char supnom[50];

printf ("Nom du contact a supprimer ? \n");
scanf ("%s", supnom);

while(i + j < nbLignes)
{
fscanf(fichsup, "%s %s %s %s\n", contactSup.nom, contactSup.prenom, contactSup.telephone, contactSup.age);
if(strcmp(supnom, contactSup.nom) != 0)
{
fprintf(fichier,"%s ",contactSup.nom);
fprintf(fichier,"%s ",contactSup.prenom);
fprintf(fichier,"%s ",contactSup.telephone);
fprintf(fichier,"%s ",contactSup.age);
fprintf(fichier,"\n");
i++;
}
else
j++;
}
fclose(fichsup);
fclose(fichier);
fichsup = fopen("adresse.txt", "w");
fichier=fopen("tampon.txt","r");
while(fscanf(fichier, "%s %s %s %s\n", contactSup.nom, contactSup.prenom, contactSup.telephone, contactSup.age)==4){
fprintf(fichsup,"%s ",contactSup.nom);
fprintf(fichsup,"%s ",contactSup.prenom);
fprintf(fichsup,"%s ",contactSup.telephone);
fprintf(fichsup,"%s ",contactSup.age);
fprintf(fichsup,"\n");}
fclose(fichsup);
fclose(fichier);

}
break;
default :

printf("\nMauvaise entree\n\n" );

break;

}



return 0;

}

Répondre à mype

4

nicochamadelol, le 12 nov 2008 à 00:34:02

Merci beaucoup tu ma bien aider.

Encore une petite question : Comment faire pour que a chaque fois que j'affiche que je supprime ou que j'ajoute un contact je retourne au menu comme cela par exemple :

nicolas@nicolas-desktop:~$ cc -o karnet karnet.c
nicolas@nicolas-desktop:~$ ./karnet

C A R N E T D ' A D R E S S E


1. Ajouter un contact
2. Afficher tout les contacts
3. Supprimer un contact

Votre choix : 2

Nom: Nom1
Prenom: prenom1
Telephone: tel1
Age: 20


Nom: Nom2
Prenom: prenom2
Telephone: tel2
Age: 21

C A R N E T D ' A D R E S S E


1. Ajouter un contact
2. Afficher tout les contacts
3. Supprimer un contact

Votre choix :



Merci d'avance et bonne journée!

Répondre à nicochamadelol

5

mype, le 12 nov 2008 à 02:13:46

Il faudrait que tu ajoutes une boucle do...while et aussi une option "quitter" dans ton menu
comme ça

#include <stdio.h>
#include <stdlib.h>

#include <string.h>


typedef struct Personne
{
char nom[50];
char prenom[50];
char telephone[16];
char age[3];
}Personne;



int main()

{
int i=0, nbLignes=0, menu = 0;
Personne contact[50];
Personne contactSup;

FILE* fichier = NULL;
FILE* fichier1 = NULL;
FILE* fich = NULL;
FILE* fichsup = NULL;
fichier = fopen("adresse.txt", "r+");
do
{
if (fichier != NULL)
{
while(fscanf(fichier, "%s %s %s %s\n", contact[nbLignes].nom, contact[nbLignes].prenom, contact[nbLignes].telephone, contact[nbLignes].age) == 4)
nbLignes++;
fclose(fichier);
}

printf("\n\t\t\tC A R N E T D ' A D R E S S E\n\n\n" );


printf("1. Ajouter un contact\n" );

printf("2. Afficher tout les contacts\n" );
printf("3. Supprimer un contact\n");
printf("4. Quitter");
printf("\n" );


printf("Votre choix : " );

scanf("%ld", &menu);




switch (menu)

{

case 1 : //ajouter contact
fichier1 = fopen("adresse.txt", "r+");

if ((fichier1 != NULL) && (nbLignes < 50))
{
while(fscanf(fichier1, "%s %s %s %s\n", contact[nbLignes].nom, contact[nbLignes].prenom, contact[nbLignes].telephone, contact[nbLignes].age) == 4)
nbLignes++;

if(nbLignes==0)
fseek(fichier1, -1, SEEK_CUR);

printf("\nNom : ");
scanf("%s", &contact[nbLignes].nom);
fprintf(fichier1, "%s ", contact[nbLignes].nom);

printf("Prenom : ");
scanf("%s", &contact[nbLignes].prenom);
fprintf(fichier1, "%s ", contact[nbLignes].prenom);

printf("Telephone : ");
scanf("%s", &contact[nbLignes].telephone);
fprintf(fichier1, "%s ", contact[nbLignes].telephone);

printf("Age : ");
scanf("%s", &contact[nbLignes].age);
fprintf(fichier1, "%s\n", contact[nbLignes].age);
fclose(fichier1);
}
else
{
printf("\nLe carnet d'adresse est plein !!\n\n");
}
break;



case 2 : //afficher contact
fich = fopen("adresse.txt", "r+");

if (fich != NULL)
{
nbLignes=0;
while(fscanf(fich, "%s %s %s %s\n", contact[nbLignes].nom, contact[nbLignes].prenom, contact[nbLignes].telephone, contact[nbLignes].age) == 4)
nbLignes++;

for(i=0;i<nbLignes;i++)
{
printf("\nNom: %s\nPrenom: %s\nTelephone: %s\nAge: %s\n\n", contact[i].nom, contact[i].prenom, contact[i].telephone, contact[i].age);
}

if( nbLignes == 0)
{
printf("Il n'y a pas de contact dans le carnet d'adresse !!!\n\n");
}
fclose(fich);
}

break;

case 3 : {//supprimer un contact

fichsup = fopen("adresse.txt", "r+");
fichier=fopen("tampon.txt","w");
int i = 0, j = 0;
char supnom[50];

printf ("Nom du contact a supprimer ? \n");
scanf ("%s", supnom);

while(i + j < nbLignes)
{
fscanf(fichsup, "%s %s %s %s\n", contactSup.nom, contactSup.prenom, contactSup.telephone, contactSup.age);
if(strcmp(supnom, contactSup.nom) != 0)
{
fprintf(fichier,"%s ",contactSup.nom);
fprintf(fichier,"%s ",contactSup.prenom);
fprintf(fichier,"%s ",contactSup.telephone);
fprintf(fichier,"%s ",contactSup.age);
fprintf(fichier,"\n");
i++;
}
else
j++;
}
fclose(fichsup);
fclose(fichier);
fichsup = fopen("adresse.txt", "w");
fichier=fopen("tampon.txt","r");
while(fscanf(fichier, "%s %s %s %s\n", contactSup.nom, contactSup.prenom, contactSup.telephone, contactSup.age)==4){
fprintf(fichsup,"%s ",contactSup.nom);
fprintf(fichsup,"%s ",contactSup.prenom);
fprintf(fichsup,"%s ",contactSup.telephone);
fprintf(fichsup,"%s ",contactSup.age);
fprintf(fichsup,"\n");}
fclose(fichsup);
fclose(fichier);

}
break;
case 4 : exit(-1);
default :

printf("\nMauvaise entree\n\n" );

break;

}
}while(menu!=4);


return 0;

}

Répondre à mype

6

nicochamadelol, le 12 nov 2008 à 11:41:11

Bonjour,

Je vous remercie beaucoup pour l'aide que vous m'avez apporté.

Il doit y avoir une erreur dans le code que vous m'avez envoyé dans le dernier post. J'ai regarder mais je n'ai pas trouvé l'erreur.
Car dans la console ca me donne ceci :

nicolas@nicolas-desktop:~$ ./kl

                        C A R N E T D ' A D R E S S E


1. Ajouter un contact
2. Afficher tout les contacts
3. Supprimer un contact
4. Quitter
Votre choix : 2

Nom: Nom1
Prenom: Prenom1
Telephone: Tel1
Age: 20

*** glibc detected *** ./kl: double free or corruption (top): 0x0804b008 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xb7e9ca85]
/lib/tls/i686/cmov/libc.so.6(cfree+0x90)[0xb7ea04f0]
/lib/tls/i686/cmov/libc.so.6(fclose+0x134)[0xb7e8b3e4]
./kl[0x8048742]
/lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe0)[0xb7e47­450]
./kl[0x80485b1]
======= Memory map: ========
08048000-0804a000 r-xp 00000000 08:01 8356764    /home/nicolas/kl
0804a000-0804b000 rw-p 00001000 08:01 8356764    /home/nicolas/kl
0804b000-0806c000 rw-p 0804b000 00:00 0          [heap]
b7d00000-b7d21000 rw-p b7d00000 00:00 0 
b7d21000-b7e00000 ---p b7d21000 00:00 0 
b7e30000-b7e31000 rw-p b7e30000 00:00 0 
b7e31000-b7f7a000 r-xp 00000000 08:01 4424375    /lib/tls/i686/cmov/libc-2.7.so
b7f7a000-b7f7b000 r--p 00149000 08:01 4424375    /lib/tls/i686/cmov/libc-2.7.so
b7f7b000-b7f7d000 rw-p 0014a000 08:01 4424375    /lib/tls/i686/cmov/libc-2.7.so
b7f7d000-b7f80000 rw-p b7f7d000 00:00 0 
b7f80000-b7f8a000 r-xp 00000000 08:01 4390938    /lib/libgcc_s.so.1
b7f8a000-b7f8b000 rw-p 0000a000 08:01 4390938    /lib/libgcc_s.so.1
b7f8b000-b7f8f000 rw-p b7f8b000 00:00 0 
b7f8f000-b7f90000 r-xp b7f8f000 00:00 0          [vdso]
b7f90000-b7faa000 r-xp 00000000 08:01 4391110    /lib/ld-2.7.so
b7faa000-b7fac000 rw-p 00019000 08:01 4391110    /lib/ld-2.7.so
bff76000-bff8b000 rw-p bffeb000 00:00 0          [stack]
Abandon
nicolas@nicolas-desktop:~$ 


Merci et bonne journée !!

Répondre à nicochamadelol

7

 mype, le 12 nov 2008 à 16:17:35

En effet j'avais mal placé le do
essaye comme ça

#include <stdio.h>
#include <stdlib.h>

#include <string.h>


typedef struct Personne
{
char nom[50];
char prenom[50];
char telephone[16];
char age[3];
}Personne;



int main()

{
int i=0, nbLignes=0, menu = 0;
Personne contact[50];
Personne contactSup;

FILE* fichier = NULL;
FILE* fichier1 = NULL;
FILE* fich = NULL;
FILE* fichsup = NULL;
do
{
nbLignes=0;
fichier = fopen("adresse.txt", "r+");

if (fichier != NULL)
{
while(fscanf(fichier, "%s %s %s %s\n", contact[nbLignes].nom, contact[nbLignes].prenom, contact[nbLignes].telephone, contact[nbLignes].age) == 4)
nbLignes++;

}
fclose(fichier);
printf("\n\t\t\tC A R N E T D ' A D R E S S E\n\n\n" );


printf("1. Ajouter un contact\n" );

printf("2. Afficher tout les contacts\n" );
printf("3. Supprimer un contact\n");
printf("4. Quitter");
printf("\n" );


printf("Votre choix : " );

scanf("%ld", &menu);



switch (menu)

{

case 1 : //ajouter contact
fichier1 = fopen("adresse.txt", "r+");

if ((fichier1 != NULL) && (nbLignes < 50))
{
while(fscanf(fichier1, "%s %s %s %s\n", contact[nbLignes].nom, contact[nbLignes].prenom, contact[nbLignes].telephone, contact[nbLignes].age) == 4)
nbLignes++;

if(nbLignes==0)
fseek(fichier1, -1, SEEK_CUR);

printf("\nNom : ");
scanf("%s", &contact[nbLignes].nom);
fprintf(fichier1, "%s ", contact[nbLignes].nom);

printf("Prenom : ");
scanf("%s", &contact[nbLignes].prenom);
fprintf(fichier1, "%s ", contact[nbLignes].prenom);

printf("Telephone : ");
scanf("%s", &contact[nbLignes].telephone);
fprintf(fichier1, "%s ", contact[nbLignes].telephone);

printf("Age : ");
scanf("%s", &contact[nbLignes].age);
fprintf(fichier1, "%s\n", contact[nbLignes].age);
fclose(fichier1);
}
else
{
printf("\nLe carnet d'adresse est plein !!\n\n");
}
break;



case 2 : //afficher contact
fich = fopen("adresse.txt", "r+");

if (fich != NULL)
{
nbLignes=0;
while(fscanf(fich, "%s %s %s %s\n", contact[nbLignes].nom, contact[nbLignes].prenom, contact[nbLignes].telephone, contact[nbLignes].age) == 4)
nbLignes++;

for(i=0;i<nbLignes;i++)
{
printf("\nNom: %s\nPrenom: %s\nTelephone: %s\nAge: %s\n\n", contact[i].nom, contact[i].prenom, contact[i].telephone, contact[i].age);
}

if( nbLignes == 0)
{
printf("Il n'y a pas de contact dans le carnet d'adresse !!!\n\n");
}
fclose(fich);
}

break;

case 3 : //supprimer un contact

fichsup = fopen("adresse.txt", "r+");
fichier=fopen("tampon.txt","w");
int i = 0, j = 0;
char supnom[50];

printf ("Nom du contact a supprimer ? \n");
scanf ("%s", supnom);
printf("nb ligne = %d",nbLignes);
while(i + j < nbLignes)
{
fscanf(fichsup, "%s %s %s %s\n", contactSup.nom, contactSup.prenom, contactSup.telephone, contactSup.age);
if(strcmp(supnom, contactSup.nom) != 0)
{
fprintf(fichier,"%s ",contactSup.nom);
fprintf(fichier,"%s ",contactSup.prenom);
fprintf(fichier,"%s ",contactSup.telephone);
fprintf(fichier,"%s ",contactSup.age);
fprintf(fichier,"\n");
i++;
}
else
j++;
}
fclose(fichsup);
fclose(fichier);
fichsup = fopen("adresse.txt", "w");
fichier=fopen("tampon.txt","r");
while(fscanf(fichier, "%s %s %s %s\n", contactSup.nom, contactSup.prenom, contactSup.telephone, contactSup.age)==4){
fprintf(fichsup,"%s ",contactSup.nom);
fprintf(fichsup,"%s ",contactSup.prenom);
fprintf(fichsup,"%s ",contactSup.telephone);
fprintf(fichsup,"%s ",contactSup.age);
fprintf(fichsup,"\n");}
fclose(fichsup);
fclose(fichier);


break;
case 4 : exit(-1); break;
default :

printf("\nMauvaise entree\n\n" );

break;

}
}while(menu!=4);


return 0;

}

Répondre à mype