RECUPERATION SUR UN FICHIER en C
Résolu
Utilisateur anonyme
-
scoubidou944 Messages postés 719 Date d'inscription Statut Membre Dernière intervention -
scoubidou944 Messages postés 719 Date d'inscription Statut Membre Dernière intervention -
Bonjour,
Je fais un programme en C mè à la récupération sur mon fichier j'obtiens des affichages bizarres ! vous sorait m'aider svp ? voici mon code :
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define N 10
#define nbre 2
typedef struct
{
char nom[N];
char prenom[N];
char pays[N];
char ville[N];
char rue[N];
char profession[N];
int code;
int gsm;
}cartevisite;
void encodecarte(cartevisite*);
void affichecarte(cartevisite*);
void enregistrefiche(cartevisite*,FILE*);
void lecturefiche(cartevisite*,FILE*);
int main()
{
cartevisite *personne;
int i;
FILE *fic;
int rep;
int drapo;
do
{
system("cls");
printf(" \n \n \t LOGICIEL DE CARTE DE VISITE \n \n ");
printf(" 1 . CREER 5 CARTES DE VISITES ET LES AFFICHER \n \n ");
printf(" 2 . AFFICHER CARTES VISITES \n \n ");
printf(" 3 . QUITTER \n \n");
scanf("%d",&rep);
if(rep==1)
{
personne=(cartevisite*)malloc(nbre*sizeof(cartevisite));
if(personne==NULL) printf(" ERREUR ! VOUS DEMANDEZ TROP DE CARTES ");
else
{
for(i=0;i<nbre;i++)
{
encodecarte(personne+i);
getch();
}
for(i=0;i<nbre;i++)
{ printf(" \n \n PERSONNE %d \n \n ",i+1);
affichecarte(personne+i);
}
enregistrefiche(personne,fic);
lecturefiche(personne,fic);
}
free(personne);
return 1;
}
else if(rep==2) lecturefiche(personne,fic);
else if(rep=3) exit(1);
else
{
printf(" TOUCHE INCORRECTE \n ");
drapo=1;
}
}while(drapo==1);
}
void encodecarte(cartevisite*personne)
{
int drapeautemporaire1,drapeautemporaire2,drapeautemporaire3,drapeautemporaire4,drapeautemporaire5,drapotmp6,drapotmp7;
char rep;
char nometprenom[N];
char espace [N]=" ";
do
{
system("cls");
do
{
printf(" ENTREZ VOTRE NOM : \t \t \n \n ");
fflush(stdin);
scanf("%s",(personne->nom));
if(strlen(personne->nom)>10) {
drapeautemporaire1=0;
printf(" CE NOM EST TROP GRAND POUR UN HUMAIN , VEUILLEZ LE DIMINUER \n \n ");
}
else drapeautemporaire1=1;
}while(! drapeautemporaire1);
do
{
printf(" \n \n ENTREZ VOTRE PRENOM : \t \t \n \n");
fflush(stdin);
scanf("%s",(personne->prenom));
if(strlen(personne->prenom)>10) {
drapeautemporaire2=0;
printf(" \n \n CE PRENOM EST TROP GRAND POUR UN HUMAIN , VEUILLEZ LE DIMINUER \n \n ");
} else drapeautemporaire2=1;
}while(! drapeautemporaire2);
strcpy(personne->nom,strcat(personne->nom,espace));
strcpy(nometprenom,(strcat(personne->nom,personne->prenom)));
printf(" \n \n BON DEBUT %s \n \n ",nometprenom);
do
{
printf(" \n \n ENTREZ VOTRE PROFESSION : \t \t \n \n ");
fflush(stdin);
scanf("%s",(personne->profession));
if(strlen(personne->profession)>10)
{
drapeautemporaire3=0;
printf(" \n \n CETTE PROFESSION N'EST PAS RECONNU , VEUILLEZ LE REVISER \n \n ");
}
else drapeautemporaire3=1;
}while(! drapeautemporaire3);
do
{
printf(" \n \n ENTREZ VOTRE VILLE : \t \t \n \n ");
fflush(stdin);
scanf("%s",(personne->ville));
if(strlen(personne->ville)>10)
{
drapeautemporaire5=0;
printf(" CETTE VILLE N'EST PAS RECONNUE , VEUILLEZ LA REVISER \n \n ");
} else drapeautemporaire5=1;
}while(!drapeautemporaire5);
do
{
printf(" \n \n ENTREZ VOTRE RUE : \t \t \n ");
fflush(stdin);
scanf("%s",(personne->rue));
if(strlen(personne->rue)>10){
drapeautemporaire4=0;
printf(" CETTE RUE N'EST PAS RECONNUE , VEUILLEZ LE REVISER \n \n ");
} else drapeautemporaire4=1;
}while(!drapeautemporaire4);
do
{
printf(" \n \n ENTREZ VOTRE CODE POSTAL : \t \t \n ");
fflush(stdin);
scanf("%d",&(personne->code));
if(personne->code>9999) {drapotmp6=0; printf(" \n CE CODE POSTAL EST INTERDIT \n \n " );}
drapotmp6=1;
}while(!drapotmp6);
do
{
printf(" \n \n ENTREZ VOTRE GSM : \t \t \n \n 04/");
fflush(stdin);
scanf("%d",&(personne->gsm));
if((personne->gsm)>9999999) {drapotmp7=0; printf(" CE GSM N'EST PAS ATTRIBUE, VEUILLEZ LE REVOIR \n " );}
else drapotmp7=1;
}while(!drapotmp7);
printf(" \n \n FORMULAIRE BIEN REMPLIE ? VOULEZ RECOMMENCER OU ENREGISTREZ ? E/R \n \n ");
fflush(stdin);
scanf("%c",&rep);
}while(rep=='R' || rep=='r');//|| drapeautemporaire3==0 || drapeautemporaire4==0 || drapeautemporaire5==0 || drapotmp6==0 || drapotmp7==0);
}
void affichecarte(cartevisite*personne)
{
printf("\n ***************************************************************** \n ");
printf(" %s %s \n",personne->nom , personne->prenom);
int compt=0;
while(compt!=4)
{
printf(" * * \n");
compt++;
}
printf("\n * %s ",personne->profession);
printf(" \n");
while(compt!=8)
{
printf(" * * \n");
compt++;
}
printf("\n * %i , RUE %s ", personne->code, personne->rue);
printf(" *\n");
printf("\n * %s", personne->ville );
printf(" 04/ %i", personne->gsm);
printf( " *\n");
printf("\n ******************************************************************* \a \n \n");
printf(" \n APPUYEZ SUR UNE TOUCHE POUR PASSER A LA SUIVANTE ");
system("color 3A");
getch();
}
void enregistrefiche(cartevisite*personne,FILE *fic)
{
int i=0;
char nomfichier[20];
printf(" \n \n \n QUELLE NOM VOULEZ POUR VOTRE FICHIER DE SAUVEGARDE \n \n ");
scanf("%s",nomfichier);
fic=fopen(nomfichier,"wb");
if(fic==NULL) printf(" \n \n ERROR TRY AGAIN \n");
else
{
for(i=0;i<nbre;i++) fwrite(personne+i,sizeof(cartevisite*),nbre,fic);
fclose(fic);
}
free(personne);
}
void lecturefiche(cartevisite*personne,FILE*fic)
{
system("cls");
char nom[123];
printf(" QUELLE FICHIER VOULEZ VOUS OUVRIR POUR LA LECTURE DES CARTES\n ");
scanf("%s",nom);
fic=fopen(nom,"rb");
if(fic==NULL) printf(" FICHIER INTROUVABLE \n ");
else
{ //nbre1=filelength(fileno(fic))/sizeof(cartevisite);
printf(" \n CE FICHIER CONTIENT %d CARTES \n",nbre);
personne=(cartevisite*)malloc(nbre*sizeof(cartevisite));
fread(personne,sizeof(cartevisite),nbre,fic);
fclose(fic);
for(int i=0;i<nbre;i++)
{
affichecarte(personne+i);
getch();
}
free(personne);
}
}
Merci d'avance
Je fais un programme en C mè à la récupération sur mon fichier j'obtiens des affichages bizarres ! vous sorait m'aider svp ? voici mon code :
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define N 10
#define nbre 2
typedef struct
{
char nom[N];
char prenom[N];
char pays[N];
char ville[N];
char rue[N];
char profession[N];
int code;
int gsm;
}cartevisite;
void encodecarte(cartevisite*);
void affichecarte(cartevisite*);
void enregistrefiche(cartevisite*,FILE*);
void lecturefiche(cartevisite*,FILE*);
int main()
{
cartevisite *personne;
int i;
FILE *fic;
int rep;
int drapo;
do
{
system("cls");
printf(" \n \n \t LOGICIEL DE CARTE DE VISITE \n \n ");
printf(" 1 . CREER 5 CARTES DE VISITES ET LES AFFICHER \n \n ");
printf(" 2 . AFFICHER CARTES VISITES \n \n ");
printf(" 3 . QUITTER \n \n");
scanf("%d",&rep);
if(rep==1)
{
personne=(cartevisite*)malloc(nbre*sizeof(cartevisite));
if(personne==NULL) printf(" ERREUR ! VOUS DEMANDEZ TROP DE CARTES ");
else
{
for(i=0;i<nbre;i++)
{
encodecarte(personne+i);
getch();
}
for(i=0;i<nbre;i++)
{ printf(" \n \n PERSONNE %d \n \n ",i+1);
affichecarte(personne+i);
}
enregistrefiche(personne,fic);
lecturefiche(personne,fic);
}
free(personne);
return 1;
}
else if(rep==2) lecturefiche(personne,fic);
else if(rep=3) exit(1);
else
{
printf(" TOUCHE INCORRECTE \n ");
drapo=1;
}
}while(drapo==1);
}
void encodecarte(cartevisite*personne)
{
int drapeautemporaire1,drapeautemporaire2,drapeautemporaire3,drapeautemporaire4,drapeautemporaire5,drapotmp6,drapotmp7;
char rep;
char nometprenom[N];
char espace [N]=" ";
do
{
system("cls");
do
{
printf(" ENTREZ VOTRE NOM : \t \t \n \n ");
fflush(stdin);
scanf("%s",(personne->nom));
if(strlen(personne->nom)>10) {
drapeautemporaire1=0;
printf(" CE NOM EST TROP GRAND POUR UN HUMAIN , VEUILLEZ LE DIMINUER \n \n ");
}
else drapeautemporaire1=1;
}while(! drapeautemporaire1);
do
{
printf(" \n \n ENTREZ VOTRE PRENOM : \t \t \n \n");
fflush(stdin);
scanf("%s",(personne->prenom));
if(strlen(personne->prenom)>10) {
drapeautemporaire2=0;
printf(" \n \n CE PRENOM EST TROP GRAND POUR UN HUMAIN , VEUILLEZ LE DIMINUER \n \n ");
} else drapeautemporaire2=1;
}while(! drapeautemporaire2);
strcpy(personne->nom,strcat(personne->nom,espace));
strcpy(nometprenom,(strcat(personne->nom,personne->prenom)));
printf(" \n \n BON DEBUT %s \n \n ",nometprenom);
do
{
printf(" \n \n ENTREZ VOTRE PROFESSION : \t \t \n \n ");
fflush(stdin);
scanf("%s",(personne->profession));
if(strlen(personne->profession)>10)
{
drapeautemporaire3=0;
printf(" \n \n CETTE PROFESSION N'EST PAS RECONNU , VEUILLEZ LE REVISER \n \n ");
}
else drapeautemporaire3=1;
}while(! drapeautemporaire3);
do
{
printf(" \n \n ENTREZ VOTRE VILLE : \t \t \n \n ");
fflush(stdin);
scanf("%s",(personne->ville));
if(strlen(personne->ville)>10)
{
drapeautemporaire5=0;
printf(" CETTE VILLE N'EST PAS RECONNUE , VEUILLEZ LA REVISER \n \n ");
} else drapeautemporaire5=1;
}while(!drapeautemporaire5);
do
{
printf(" \n \n ENTREZ VOTRE RUE : \t \t \n ");
fflush(stdin);
scanf("%s",(personne->rue));
if(strlen(personne->rue)>10){
drapeautemporaire4=0;
printf(" CETTE RUE N'EST PAS RECONNUE , VEUILLEZ LE REVISER \n \n ");
} else drapeautemporaire4=1;
}while(!drapeautemporaire4);
do
{
printf(" \n \n ENTREZ VOTRE CODE POSTAL : \t \t \n ");
fflush(stdin);
scanf("%d",&(personne->code));
if(personne->code>9999) {drapotmp6=0; printf(" \n CE CODE POSTAL EST INTERDIT \n \n " );}
drapotmp6=1;
}while(!drapotmp6);
do
{
printf(" \n \n ENTREZ VOTRE GSM : \t \t \n \n 04/");
fflush(stdin);
scanf("%d",&(personne->gsm));
if((personne->gsm)>9999999) {drapotmp7=0; printf(" CE GSM N'EST PAS ATTRIBUE, VEUILLEZ LE REVOIR \n " );}
else drapotmp7=1;
}while(!drapotmp7);
printf(" \n \n FORMULAIRE BIEN REMPLIE ? VOULEZ RECOMMENCER OU ENREGISTREZ ? E/R \n \n ");
fflush(stdin);
scanf("%c",&rep);
}while(rep=='R' || rep=='r');//|| drapeautemporaire3==0 || drapeautemporaire4==0 || drapeautemporaire5==0 || drapotmp6==0 || drapotmp7==0);
}
void affichecarte(cartevisite*personne)
{
printf("\n ***************************************************************** \n ");
printf(" %s %s \n",personne->nom , personne->prenom);
int compt=0;
while(compt!=4)
{
printf(" * * \n");
compt++;
}
printf("\n * %s ",personne->profession);
printf(" \n");
while(compt!=8)
{
printf(" * * \n");
compt++;
}
printf("\n * %i , RUE %s ", personne->code, personne->rue);
printf(" *\n");
printf("\n * %s", personne->ville );
printf(" 04/ %i", personne->gsm);
printf( " *\n");
printf("\n ******************************************************************* \a \n \n");
printf(" \n APPUYEZ SUR UNE TOUCHE POUR PASSER A LA SUIVANTE ");
system("color 3A");
getch();
}
void enregistrefiche(cartevisite*personne,FILE *fic)
{
int i=0;
char nomfichier[20];
printf(" \n \n \n QUELLE NOM VOULEZ POUR VOTRE FICHIER DE SAUVEGARDE \n \n ");
scanf("%s",nomfichier);
fic=fopen(nomfichier,"wb");
if(fic==NULL) printf(" \n \n ERROR TRY AGAIN \n");
else
{
for(i=0;i<nbre;i++) fwrite(personne+i,sizeof(cartevisite*),nbre,fic);
fclose(fic);
}
free(personne);
}
void lecturefiche(cartevisite*personne,FILE*fic)
{
system("cls");
char nom[123];
printf(" QUELLE FICHIER VOULEZ VOUS OUVRIR POUR LA LECTURE DES CARTES\n ");
scanf("%s",nom);
fic=fopen(nom,"rb");
if(fic==NULL) printf(" FICHIER INTROUVABLE \n ");
else
{ //nbre1=filelength(fileno(fic))/sizeof(cartevisite);
printf(" \n CE FICHIER CONTIENT %d CARTES \n",nbre);
personne=(cartevisite*)malloc(nbre*sizeof(cartevisite));
fread(personne,sizeof(cartevisite),nbre,fic);
fclose(fic);
for(int i=0;i<nbre;i++)
{
affichecarte(personne+i);
getch();
}
free(personne);
}
}
Merci d'avance
A voir également:
- RECUPERATION SUR UN FICHIER en C
- Fichier bin - Guide
- Comment réduire la taille d'un fichier - Guide
- Comment ouvrir un fichier epub ? - Guide
- Fichier rar - Guide
- Fichier .dat - Guide
1 réponse
Bonsoir,
plusieurs choses sur ton code.
1) Tu utilises une vieille norme de langage C.
Si tu bosses avec Visual Studio 2010 Express, tu as des fonctions qu'ils doit d'indiquer comme dépréciées :
strcpy => strcpy_s
(c'est un exemple. Cela permet d'éviter les débordement de buffer)
2) ensuite, pense à prendre l'habitude de resserrer tes typages de variables, ça évite bien des erreurs.
Par exemple, enregistrer ta fiche ne nécessite pas de modifier l'objet Fiche en question donc :
void enregistrefiche(cartevisite*personne,FILE *fic)
{
}
devient
#define IN
#define OUT
void enregistrefiche (IN const cartevisite*personne, IN OUT FILE *fic)
{
}
3) Les normes de nommage des variables. Tous les développeurs codent en Anglais sur le format Camel/Pascal, fais un peu de recherche sur Google.
La notation hongroise est très décriée quand à elle.
4) Tu utilises à la fois stdin et scanf. Choisis l'un ou l'autre, là tu mixtes 2 méthodes de traitement de flux c'est à éviter.
5) Tu travailles sur ton fichier en mode binaire et non pas en mode texte. C'est peut-être volontaire mais regarder bien avec un éditeur hexadécimal s'il enregistre bien ce qu'il faut.
voilà pour commencer ;p
plusieurs choses sur ton code.
1) Tu utilises une vieille norme de langage C.
Si tu bosses avec Visual Studio 2010 Express, tu as des fonctions qu'ils doit d'indiquer comme dépréciées :
strcpy => strcpy_s
(c'est un exemple. Cela permet d'éviter les débordement de buffer)
2) ensuite, pense à prendre l'habitude de resserrer tes typages de variables, ça évite bien des erreurs.
Par exemple, enregistrer ta fiche ne nécessite pas de modifier l'objet Fiche en question donc :
void enregistrefiche(cartevisite*personne,FILE *fic)
{
}
devient
#define IN
#define OUT
void enregistrefiche (IN const cartevisite*personne, IN OUT FILE *fic)
{
}
3) Les normes de nommage des variables. Tous les développeurs codent en Anglais sur le format Camel/Pascal, fais un peu de recherche sur Google.
La notation hongroise est très décriée quand à elle.
4) Tu utilises à la fois stdin et scanf. Choisis l'un ou l'autre, là tu mixtes 2 méthodes de traitement de flux c'est à éviter.
5) Tu travailles sur ton fichier en mode binaire et non pas en mode texte. C'est peut-être volontaire mais regarder bien avec un éditeur hexadécimal s'il enregistre bien ce qu'il faut.
voilà pour commencer ;p
Mais voilà je travaille sur MingW cè un devoir à faire pour acquiert de bonnes perfomances ! avec les flux tu m'avances bcp ! merci mais les fonctions je suis obligé de les garder et les fichiers binaires ossi !
Quand tu utilises une librairie qui n'est pas de toi, il est parfois difficile de savoir si ton objet est modifié à l'intérieur ou non.
Le mot clé 'const' permet justement de dire qu'il sera utilisé en Read Only.
Bonne chance