no save
Assistance
Achat
News

Forum | programmation
[Langage C] Problème supprimer contact carnet
nicochamadelol, le sam. 08 nov. 2008 à 17:42:31
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


nicochamadelol
nov. 08
Suivant
REPONSES
nicochamadelol
nov. 08
nicochamadelol
nov. 08
mype
nov. 08
nicochamadelol
nov. 08
mype
nov. 08
nicochamadelol
nov. 08
mype
nov. 08
Version Web
Réalisé par RedShift
no save