2017-03-24 21:53:41 +01:00
|
|
|
/*
|
|
|
|
* EX18
|
|
|
|
* VAN DAMME Adrien
|
|
|
|
* Grp 2
|
|
|
|
* La fct fgets permet de récuperer une chaine de caractère de façon sécurisée
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <conio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#define __STR_LENGHT__ 20
|
|
|
|
|
|
|
|
int EncoderContact(char *, char *, char *, char *, char *);
|
|
|
|
int EcrireContact(char *, char *, char *, char *, char *, FILE *);
|
|
|
|
FILE* OuvrirFichier();
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
char Nom[__STR_LENGHT__],Prenom[__STR_LENGHT__],Tel[__STR_LENGHT__],GSM[__STR_LENGHT__],eMail[__STR_LENGHT__];
|
|
|
|
FILE * fi;
|
|
|
|
fi = OuvrirFichier();
|
|
|
|
|
|
|
|
while(EncoderContact(Nom, Prenom, Tel, GSM, eMail)==-1);
|
|
|
|
while(EcrireContact(Nom, Prenom, Tel, GSM, eMail, fi)==-1);
|
|
|
|
fclose(fi);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int EncoderContact(char *nom, char *prenom, char *tel, char *gsm, char *email)
|
|
|
|
{
|
|
|
|
int bug = 0;
|
|
|
|
char *p;
|
|
|
|
printf("Votre nom (%d caratères max)", (int)__STR_LENGHT__);
|
|
|
|
fflush(stdin);bug = fgets(nom, __STR_LENGHT__, stdin)?bug:-1;
|
|
|
|
p = strchr(nom, '\n');
|
|
|
|
if(p)*p=0;
|
|
|
|
|
|
|
|
printf("Votre prenom (%d caratères max)", (int)__STR_LENGHT__);
|
|
|
|
fflush(stdin);bug = fgets(prenom, __STR_LENGHT__, stdin)?bug:-1;
|
|
|
|
p = strchr(prenom, '\n');
|
|
|
|
if(p)*p=0;
|
|
|
|
|
|
|
|
printf("Votre telephone (%d caratères max)", (int)__STR_LENGHT__);
|
|
|
|
fflush(stdin);bug = fgets(tel, __STR_LENGHT__, stdin)?bug:-1;
|
|
|
|
p = strchr(tel, '\n');
|
|
|
|
if(p)*p=0;
|
|
|
|
|
|
|
|
printf("Votre gsm (%d caratères max)", (int)__STR_LENGHT__);
|
|
|
|
fflush(stdin);bug = fgets(gsm, __STR_LENGHT__, stdin)?bug:-1;
|
|
|
|
p = strchr(gsm, '\n');
|
|
|
|
if(p)*p=0;
|
|
|
|
|
|
|
|
printf("Votre email (%d caratères max)", (int)__STR_LENGHT__);
|
|
|
|
fflush(stdin);bug = fgets(email, __STR_LENGHT__, stdin)?bug:-1;
|
|
|
|
p = strchr(email, '\n');
|
|
|
|
if(p)*p = 0;
|
|
|
|
|
|
|
|
return bug;
|
|
|
|
}
|
|
|
|
int EcrireContact(char *nom, char *prenom, char *tel, char *gsm, char *email, FILE *f)
|
|
|
|
{
|
|
|
|
int bug = 0;
|
|
|
|
clearerr(f);
|
|
|
|
fprintf(f, "Nom %s\n", nom);
|
|
|
|
bug = ferror(f)?-1:bug;
|
|
|
|
fprintf(f, "Prenom %s\n", prenom);
|
|
|
|
bug = ferror(f)?-1:bug;
|
|
|
|
fprintf(f, "Telephone %s\n", tel);
|
|
|
|
bug = ferror(f)?-1:bug;
|
|
|
|
fprintf(f, "GSM %s\n", gsm);
|
|
|
|
bug = ferror(f)?-1:bug;
|
|
|
|
fprintf(f, "Email %s\n", email);
|
|
|
|
bug = ferror(f)?-1:bug;
|
|
|
|
|
|
|
|
return bug;
|
|
|
|
}
|
|
|
|
FILE* OuvrirFichier()
|
|
|
|
{
|
|
|
|
FILE *fi;
|
|
|
|
char chemin[__STR_LENGHT__];
|
|
|
|
do{
|
|
|
|
do{
|
|
|
|
printf("Emplacement de votre fichier (%d caratères max)", (int)__STR_LENGHT__);
|
|
|
|
fflush(stdin);
|
|
|
|
}while(fgets(chemin, __STR_LENGHT__, stdin) == NULL);
|
|
|
|
*(strchr(chemin, '\n')) = 0;
|
|
|
|
fi = fopen(chemin, "w");
|
|
|
|
}while(fi == NULL);
|
|
|
|
return fi;
|
|
|
|
}
|