|
|
|
|
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
Bonjour,
|
Il faudrait que tu utilise un fichier tapon pour supprimer un contact...
#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;
}
|
Merci beaucoup tu ma bien aider.
|
Il faudrait que tu ajoutes une boucle do...while et aussi une option "quitter" dans ton menu
#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;
}
|
Bonjour,
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)[0xb7e47450]
./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 !! |
En effet j'avais mal placé le do
#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;
} |