Phonebook – The c language files

[wptab name=’Exercise’]

It is assumed that a person is characterized by the first name, last name and telephone number. Our goal is to create a phonebook, we can define a peronne structure that contains two string and an integer variable for phone numbers.

  1. Write a function void ajout_personne (FILE * rep) which enters a person and adds it to the end of the file passed as parameter
  2. Write a function void afficher_ensemble_personne (FILE * rep) which displays the contents of the file.
  3. Write a function void trouve_numéro_personne (FILE * rep, char * name) to find the phone number of a given person as a parameter.
  4. Write a function void void changer_numéro_personne  (FILE * rep, char * name) to change the telephone number of a given person as a parameter.
  5. Write a void void menu(FILE *rep)that asks for the operation to be performed: add a person, display the contents of the directory, find a number or exit the program.
  6. Write the main program

Notes:
We work with files of type DAT: repertoire.dat
The function fseek (f1, 0, SEEK_SET); Allows you to place the cursor at the beginning of the file.
Instead of using the fprint function for writing to the directory.dat file, you will use the fwrite function as follows:

fprintf( fichier , "%d\n" , personne.age);
fprintf( fichier , "%s\n" , personne.nom);
fprintf( fichier , "%s\n" , personne.prenom);
fprintf( fichier , "%s\n" , personne.adresse);
fprintf( fichier , "%d\n" , personne.nombreFreres);

Is replaced by the instruction :

fwrite( &personne , sizeof(personne) , 1 , fichier);

For reading, we will use the function fread as follows:
Let’s take a simple example: If we have a variable ‘var’ that we saved by fwrite in file ‘file’ then we can load it like this:

int var;
fread( &var , sizeof(var) , 1 , fichier );

For structure Person:

personne pers;
FILE *rep
fread(&pers,sizeof(personne),1,rep)

The following code checks whether the fread function has read data or not

while (fread(&pers,sizeof(personne),1,rep) != 0) {
traitement.....
}

Fread can not be used if the file was created with the functions fputs, fprintf or other functions writing in formatted mode.
[/wptab]
[wptab name=’Solution’]

#include <stdio.h>
#include <string.h>

typedef struct
{
char nom[10];
char prenom[10];
int tel;
} personne;

void ajout_personne (FILE *rep);
void affiche_ensemble_personne(FILE *rep);
void trouve_numero_personne(FILE *rep, char *nom);
void menu(FILE *rep);

void ajout_personne (FILE *rep) {
personne pers;
printf ("nom : ");
scanf ("%s", pers.nom);
printf ("prenom : ");
scanf ("%s", pers.prenom);
printf ("tel : ");
scanf ("%d", &pers.tel);
fwrite (&pers, sizeof(pers), 1, rep);
}

void affiche_ensemble_personne(FILE *rep) {
personne pers;

fseek(rep,0,SEEK_SET);
while (fread(&pers,sizeof(personne),1,rep) != 0) {
printf("nom .....: %s\n",pers.nom);
printf("prenom ..: %s\n",pers.prenom);
printf("tel .....: %d\n\n",pers.tel);
}
}

void trouve_numero_personne(FILE *rep, char *nom) {
personne pers;
int trouve = 1;

printf("Donnez le nom : ");
scanf("%s",nom);
fseek(rep, 0, SEEK_SET);
while (fread(&pers, sizeof(personne), 1, rep) != 0 && trouve != 0) {
if ((trouve = strcmp(pers.nom, nom)) == 0)
printf("Tel de %s %s : %d\n\n",pers.prenom, pers.nom, pers.tel);
}
if (trouve != 0) {
printf("Ce nom n'existe pas\n");
}
}

void menu(FILE *rep) {
char reponse, nom[10];
do {
printf("AJOUTER.... : A\n");
printf("LISTER..... : L\n");
printf("CHERCHER....: T\n");
printf("QUITER......: Q\n");
printf(" votre choix: ");
scanf(" %c",&reponse);
switch(reponse) {
case 'a':case 'A': ajout_personne (rep);
break;
case 'l':case 'L': affiche_ensemble_personne(rep);
break;
case 't':case 'T': trouve_numero_personne(rep, nom);
}
}
while( reponse != 'q' && reponse != 'Q');
}

int main () {
FILE *repertoire;

repertoire = fopen ("reper.dat", "a+");
menu(repertoire);
fclose (repertoire);
return 0;
}

[/wptab]

 

[end_wptabset]