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 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
| #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
//Definition du type.
struct Enreg{
char nom[40];
char tel[10];
};
//prototypes des fonctions
int menu(void);
void creer(struct Enreg fiche);
void recherche(struct Enreg fiche);
FILE *fd;
void saisi_data(struct Enreg *fiche);
//programme ppal
int main()
{
/*déclaration et initialisation
des varoables*/
struct Enreg fiche={0, 0};
int choix;
/*Menu*/
do{
choix=menu();
switch(choix){
case 1:
creer(fiche);
printf("\n");
continue;
case 2:
recherche(fiche);
printf("\n");
continue;
default:
printf("Au revoir...\n\n");
}
} while(choix!=3);
system("PAUSE");
}
/*Programme affichant le menu*/
int menu(void)
{
int i;
do{
printf("\nMenu Principal\n");
printf(" 1 - Creation\n");
printf(" 2 - Recherche\n");
printf(" 3 - Fin\n");
printf("Indiquez l'op choisie(1. 2. 3.): ");
scanf("%d", &i);
/*Tant que la valeur ne correspond pas
au choix proposés au menu affiche: Error!*/
if(i<1 || i>3)
printf("\nErr recommencez\n");
}while(i<1 || i>3);
printf("\n");
return(i);
}
/*programme permettant de creer une fiche de renseignement*/
void creer(struct Enreg fiche){
int rep=0; //valeur de test
printf("Sous-Programme création dune fiche.\n");
printf("Appuyez sur une touche...\n");
getch();
saisi_data(&fiche); /*Appel de la fonction
de saisie des valeurs*/
printf("Voulez vous conserveez cette fiche(1=yes//0=no): ");
scanf("%d", &rep);
if(rep==1){
fd=fopen("repertoire.bin", "a+");
if(fd==NULL){
printf("\nError lors de l/'ouverture du fichier spécifié.!\n");
}
else{
fseek(fd, 0, SEEK_END);
fwrite(&fiche, sizeof(struct Enreg), 1, fd);
fclose(fd);
printf("Sauvegarde reussie!\n");
}
}
else{
fclose(fd);
printf("Echec sauvegarde!\n");
}
return;
}
/*programme de recherche*/
void recherche(struct Enreg fiche){
char mot[40]={0};
int rep;
printf("Sous-Programme recherche dune fiche.\n");
printf("Appuyez sur une touche...\n");
getch();
printf("\nEntrer le nom recherché: ");
scanf("%s", &mot);
fd=fopen("repertoire.bin", "r");
if(fd==NULL){
printf("Echec lors de l'ouverture du fichier");
}
else{
fseek(fd, 0, SEEK_SET);
do{
fread(&fiche, sizeof(struct Enreg), 1, fd);
if(strcmp(fiche.nom, mot)==0){
printf("%s ", fiche.nom);
printf(" %s\n", fiche.tel);
printf("Continuer la rechercher?(0=oui/1=non): ");
scanf("%d", &rep);
if(rep!=0) break;
}
if(feof(fd)){
printf("\n\nFin du fichier\nAppuyez sur une touche...\n");
getch();}
} while(!feof(fd));
fclose(fd);
}
}
/*programme de saisie des valeur*/
void saisi_data(struct Enreg *fiche){
char c;
int i;
int cpt = 0;
printf("Entrez le nom: ");
fflush(stdout);
for (i = 0; i < sizeof fiche->nom - 1 && (c = getchar()) != '\n' && c != EOF; i++)
{
fiche->nom[i] = toupper(c);
cpt++;
}
printf("Entre le Numero: ");
scanf("%s", &fiche->tel);
return;
} |
Partager