Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
1500 connectés 

  FORUM HardWare.fr
  Programmation
  C

  probleme d ecriture dans un fichier

 

Sujet(s) à lire :
    - Visual C++
 

 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

probleme d ecriture dans un fichier

n°1012571
jon_w748
Posté le 14-03-2005 à 21:08:11  profilanswer
 

voila j ai un probleme je n arrive pas a ecrire dans mon fichier le programme plante lors du fwrite si qq un pouvait merci d avance ;)!!!
 
voici le code:
 
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void testfichier(FILE *,FILE *);
void ajout(struct FICHE *pt,FILE *);
void tri(struct INDEX *,int);
void insertion(struct INDEX *,int,struct INDEX *);
struct FICHE{
           int matr;
     char nom[20];
     char prenom[15];
   };  
 
struct INDEX{
           char nom [20];
     long pos;
   };
 
 
void main()
{
 struct FICHE membre;
    struct INDEX index;
 int choix;
 int i=1;
    FILE *fp;/*pointeur du fichier membre.dat*/
 FILE *fpi;/*pointeur du fichier d index*/
    while(i>0)
 {
      printf(" ------------------MENU-----------------\n" );  
      printf("|              1:encodage               |\n" );
      printf("|              2:quitter                |\n" );
      printf(" ---------------------------------------\n" );
      printf(" choissisez l option desirez:" );
      choix=getchar();
   printf("\n" );    
   switch(choix)
   {
     case'1':testfichier(fp,fpi);
       ajout(&membre,fp);
 
     break;  
  case'2':i=-1;
    }      
  fflush(stdin);
 }
 fcloseall();
}
/************************************************************************
*I:ADRESSE DE DEBUT DE TABLEAU ET NOMBRE DE FICHE                       *
*P:TRI PAR VECTEUR D INDEX                                              *
*O:---------------------------------------------------------------------*
************************************************************************/
void testfichier (FILE *fp,FILE *fpi)
{
 int choix,i=1;
 fp = fopen ("C:\\fichier\\membre.dat", "r+b" );
 if (fp == NULL)
 {
     printf("le fichier n a pas pu etre ouvert ou n existe pas\n" );
  printf("CREATION DU FICHIER !!!!!\n" );
     fp = fopen ("C:\\fichier\\membre.dat", "wb" );
     fpi = fopen ("C:\\fichier\\membre.ndx", "wb" );
     printf("le fichier a ete cree\n\n" );
         
 }      
    else
 {
  if (fp != NULL)
  {
  printf("le fichier existe deja\n\n" );
  fflush(stdin);
        while(i>0)
  {
         printf(" ------------------MENU-----------------\n" );  
         printf("|    1:creation du nouveaux fichier     |\n" );
         printf("|    2:garder le fichier present        |\n" );
         printf(" ---------------------------------------\n" );
         printf(" choissisez l option desirez:" );
         choix=getchar();
      printf("\n" );    
      switch(choix)
   {
        case'1':fp = fopen ("C:\\fichier\\membre.dat", "wb" );
          fpi = fopen ("C:\\fichier\\membre.ndx", "wb" );
          printf("le nouveau fichier a ete cree\n" );
       i=-1;
                   break;  
     case'2':i=-1;
       
         
   }      
       fflush(stdin);
  }
  }
 }
 
}
/************************************************************************
*I:ADRESSE DE DEBUT DE TABLEAU ET NOMBRE DE FICHE                       *
*P:TRI PAR VECTEUR D INDEX                                              *
*O:---------------------------------------------------------------------*
************************************************************************/
void ajout(struct FICHE *pt, FILE *fp)
{
   printf("entrez le matricule:\n" );
   fflush(stdin);
   scanf("%d",&pt->matr);
      printf("entrez le nom\n" );
   fflush(stdin);
   gets(pt->nom);
   printf("entrez le prenom\n" );
   fflush(stdin);
   gets(pt->prenom);
   system("cls" );
   fwrite(&pt,sizeof(pt),1,fp);
}
voila c est dans la fonction ajout que ca plante au moment du fwrite mais je ne trouve pas mon erreur :'(
merci d avance

mood
Publicité
Posté le 14-03-2005 à 21:08:11  profilanswer
 

n°1012584
didier1809
${citation_perso}
Posté le 14-03-2005 à 21:38:58  profilanswer
 

et en mettant sizeof(*pt) ?
 
a moins que je dise une connerie énorme  :D  
 
et fflush(stdin), c'est mal  :o , fflush n'a pas de comportemetn bien défini quand il travaille sur stdin


---------------
.
n°1012588
jon_w748
Posté le 14-03-2005 à 21:45:37  profilanswer
 

non j ai trouve mon erreur en regardant sur un site je dois faire un fopen au debut de ma fonction d ajout alors que je n ai pas fait de fcolse dans test :s le fichier reste ouvert mais ....

n°1012593
Emmanuel D​elahaye
C is a sharp tool
Posté le 14-03-2005 à 21:51:29  profilanswer
 

jon_w748 a écrit :

voila j ai un probleme je n arrive pas a ecrire dans mon fichier le programme plante lors du fwrite si qq un pouvait merci d avance ;)!!!
 
voici le code:


Déjà, ça compile pas...


main.c:5: warning: `struct FICHE' declared inside parameter list
main.c:5: warning: its scope is only this definition or declaration, which is probably not what you want
main.c:6: warning: `struct INDEX' declared inside parameter list
main.c:7: warning: `struct INDEX' declared inside parameter list
 
main.c:21: warning: function declaration isn't a prototype
main.c:21: warning: return type of `main' is not `int'
main.c: In function `main':
main.c:23: warning: declaration of `index' shadows a global declaration
<built-in>:0: warning: shadowed declaration is here
main.c:40: warning: passing arg 1 of `ajout' from incompatible pointer type
main.c:47: warning: implicit declaration of function `fcloseall'
main.c:23: warning: unused variable `index'
main.c:26: warning: `fp' might be used uninitialized in this function
main.c:27: warning: `fpi' might be used uninitialized in this function
 
main.c: At top level:
main.c:105: conflicting types for `ajout'
main.c:5: previous declaration of `ajout'


Ensuite, entre scanf() et gets(), les saisies sont extrêmement fragiles.
 
http://mapage.noos.fr/emdel/notes.htm#saisie
 
Enfin, la gestion des fichiers est très douteuse...
 
http://mapage.noos.fr/emdel/notes.htm#fichiers


Message édité par Emmanuel Delahaye le 14-03-2005 à 21:57:02

---------------
Des infos sur la programmation et le langage C: http://www.bien-programmer.fr Pas de Wi-Fi à la maison : http://www.cpl-france.org/

Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  C

  probleme d ecriture dans un fichier

 

Sujets relatifs
spécification des droits lors de la création d'un fichierFichier pour login/pass en flash
probleme pour modifier mes news[C#] connaitre le nombre de ligne sans parcourir fichier
probleme ecriture fichierProblème de noob ecriture/lecture fichier texte
Problème écriture dans fichier texteEcriture dans un fichier XLS : problème de format
[C++ Builder] Problème étrange entre OpenDialog & écriture de fichiertoujours mon problème d'écriture dans un fichier
Plus de sujets relatifs à : probleme d ecriture dans un fichier


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR