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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
| /*
Description:
améliore la saisie des noms et de l'enregistrement dans
un fichier pour gérer les espaces*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include<conio.h>
FILE *fd;
{
char nom[30];
char tel[10];
};
int question(char *texte)
{
char reponse=' ';
while (reponse!='o' && reponse!='n')
{
printf("%s",texte);
/*attente d'un caractére au clavier*/
reponse=getch();
printf("\n");
/*convertion des majuscules en minuscules*/
if (reponse=='O') reponse='o';
if (reponse=='N') reponse='n';
if (reponse!='o' && reponse!='n')
printf("!!!repondre par O ou N!!!\n");
};
if (reponse=='o')
return 0;
else return -1;
}
/*création d'une nouvelle fiche*/
void creation()
{
struct enreg fiche;
int reponse;
/*demande du nom et du numero de téléphone*/
printf("...Creation d'une fiche...\n");
printf("Nom ? ");
scanf("%s",&fiche.nom);
printf("Numero de telephone ? ");
scanf("%s",&fiche.tel);
/*Demande confirmation d'enregistrement*/
reponse=question("Enregistrer la fiche (O/N) ?");
/*si NON, on sort directement*/
if (reponse)return;
/*se placer à la fin du fichier*/
fseek(fd,0,2);
/*sinon on enregistre à la fin du fichier*/
fprintf(fd,"%s%s\n",fiche.nom,fiche.tel);
}
int rechercher()
{
/*variable locales*/
struct enreg fiche;
char nom[30];
int err;
int reponse;
/*demande le nom à rechercher*/
printf("...rechercher une fiche...\n");
printf("nom a chercher ? ");
scanf("%s",&nom);
/*positionnement au début du fichier*/
fseek(fd,0,0);
/*lecture d'une fiche*/
err=fscanf(fd,"%s%s",&fiche.nom,&fiche.tel);
/*si fin du fichier ou erreur*/
while (err>0)
{
/*si la fiche correspond*/
if (strcmp(&fiche.nom,&nom)==0)
{
/*on affiche la fiche*/
printf("nom:%s telephone:%s \n",fiche.nom,fiche.tel);
/*on demande à continuer*/
reponse=question("continuer la recherche (O/N)?");
/*si l'on arrete, on sort de la fonction*/
if (reponse)return ;
}
/*on lit une autre fiche*/
err=fscanf(fd,"%s%s",&fiche.nom,&fiche.tel);
}
/*fin*/
printf("fin du fichier, appuyer sur une touche...\n");
getch();
}
/*affiche le menu générale et attend l'entrée d'un choix*/
int menu()
/*fonction qui affiche le menu et attend un choix correcte*/
{
int choix=0;
while (choix<1 || choix>3)
{/*Affichage du menu (les différent choix)*/
printf("menu : \n");
printf("1 - creation d'une nouvelle fiche\n");
printf("2 - recherche d'une fiche\n");
printf("3 - quitter\n");
printf("votre choix ? ");
scanf("%d",&choix);
if (choix<1 || choix>3)
printf("!!!entrez 1, 2 ou 3 !!!\n");
}
return choix;
}
/*lecture d'une ligne avec espace au clavier*/
void lecture(char *texte)
{
char c=0;
c=getch();
while (c!=13)
{
/*si c'est une majuscule, on la passe en minuscule*/
if (isupper(c)) c=tolower(c);
/*retour arriere oui ou non*/
if (c==8)
{
/*oui on recule dans le tableau*/
texte--;
/*et on efface le dernier caractére à l'écran*/
printf(" \b");
}
/*non on sauve le caractére et on avance*/
else
*texte++=c;
c=getch();
};
/* ajoute le zero terminale*/
*texte='\0';
/*affiche un retour chariot pour ramener le curseur à la ligne*/
printf("\n");
}
/*programme principale*/
int main()
{
int choix=0;
fd=fopen("rep.dat","a+");
/*Affichage de ...REPERTOIRE TELEPHONIQUE...*/
printf("...REPERTOIRE TELEPHONIQUE...\n");
/*tant que choix quitter n'est pas choisi,retour au menu*/
while (choix!=3)
{
choix=menu();
switch (choix)
{
case 1 : creation();
break;
case 2 : rechercher();
break;
default : break;
}
}
fclose(fd);/*Fermeture du fichier*/
/*Fin*/
printf("Merci, et au revoir\n");
scanf("%d",&choix);
} |
Partager