Probleme avec le fwrite et les listes chainés
Bonjour , je suis actuellement de faire un programme permettant de gerer la gestion des stocks d'un magasin de composants electronique , ne connaissant pas le nombre de composant a l'avance , ni leur nom , fabricant , fonction et prix , je n'utilise AUCUN tableau mais plutot des listes chainé.
Voila comment j'enregistre ces quatres renseigments dans une liste chainé:
Code:
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
|
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <malloc.h>
struct composant{ char NOM[20];
char FABRICANT[20];
char FONCTION[100];
float PRIX;
composant *adresse;
};
struct composant *ter;
composant InsertionEnAd(char Inom[20],char Ifabricant[20],char Ifonction[100],float Iprix,struct composant *adsuivante);
composant InsertionEnAd(char Inom[20],char Ifabricant[20],char Ifonction[100],float Iprix,struct composant *adsuivante)
{
struct composant *ad;
ad = (struct composant *)malloc (sizeof(struct composant)); // on force le type de retour en adresse avec
if (ad!=NULL) // (struct wagon *)
{ // calcul du nbr d'octect d'1 wagon : sizeof(struct wagon)
strcpy((*ad).NOM , Inom);
strcpy((*ad).FABRICANT , Ifabricant);
strcpy((*ad).FONCTION , Ifonction);
(*ad).PRIX , Iprix;
//(*ad).adresse = adsuivante;
//adsuivante = ad;
ter = ad;
//printf("%s %s %s %f",ad->NOM,ad->FABRICANT,ad->FONCTION,ad->PRIX);
}
return *ad;
}
void main(void)
{
int n=0;
int choix;
char Snom[20];
char Sfabricant[20];
char Sfonction[100];
float Sprix;
printf("Saisir Nom du composant\n");
scanf("%s",&Snom);
printf("Saisir Fabricant du composant\n");
scanf("%s",&Sfabricant);
printf("Saisir Fonction du composant\n");
scanf("%s",&Sfonction);
printf("Saisir Prix du composant\n");
scanf("%f",&Sprix);
InsertionEnAd(Snom,Sfabricant,Sfonction,Sprix,ter);
getch()
} |
Une fois cela fais , il faudrait que je puisse enregistrer cette liste chainé , pour cela j'utilise ma fonction Sauvegarder() :
Code:
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
|
void Sauvegarder(struct composant *Sad);
void Sauvegarder(struct composant *Sad)
{
FILE *Fich;
int fermer;
/* Ouverture pour ecriture */
if((Fich = fopen( "c:\\fichier.txt", "w+" )) == NULL )
printf( "Le Fichier na pas pu etre ouvert\n" );
else
{
printf( "Le Fichier est ouvert\n" );
/* Ecriture */
fwrite(Sad->NOM, sizeof(char), 20, Fich);
fwrite(Sad->FABRICANT, sizeof(char), 20, Fich);
fwrite(Sad->FONCTION, sizeof(char), 100, Fich);
//fwrite(Sad->PRIX, sizeof(float), 20, Fich);
}
fermer = _fcloseall();
fclose(Fich);
if(fermer>0)
printf( "Fichier Fermer");
else
printf( "Fichier NON Fermer");
} |
Le fichier c:\fichier.txt est bien créé mais voila ce qu'il contient :
Citation:
敲楳瑳湡散촀췍췍췍췍楳浥湥s췍췍췍췍췍췍档污略r췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍췍
enfin , il contient un peu n'importe quoi...
donc si vous voyez d'ou mon probléme vient , j'aprécierez bcp votre aide.
Amicalement lucas
Ami programmeur