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
|
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <malloc.h>
struct composant{ char NOM[20]; //Création d'une structure COMPOSANT comportant 4 éléments
char FABRICANT[20];
char FONCTION[100];
float PRIX;
composant *adresse;
};
struct composant * ter = NULL;
/*void CreerListeVide(void);
void CreerListeVide(void)
{
ter=NULL;
}*/
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 *adsuivant;
adsuivant = (struct composant *)malloc (sizeof(struct composant));
struct composant *adapres;
adapres = (struct composant *)malloc (sizeof(struct composant));
if (adsuivant!=NULL)
{
strcpy((*adsuivant).NOM , Inom);
strcpy((*adsuivant).FABRICANT , Ifabricant);
strcpy((*adsuivant).FONCTION , Ifonction);
(*adsuivant).PRIX = Iprix;
//(*ad).adresse = adsuivante;
//adsuivante = ad;
ter = adsuivant;
adapres=NULL;
}
return *adsuivant;
}
void Afficher(struct composant *Aad);
void Afficher(struct composant *Aad)
{//do{
if(Aad!=NULL)
printf("\nNom : %s\nFabricant : %s\nFonction : %s\nPrix HT : %f Euros\n",Aad->NOM,Aad->FABRICANT,Aad->FONCTION,Aad->PRIX);
else
printf(" !Aucun composant(s) present dans la liste chaine! \n");
//}while(Aad!=NULL);
}
void SauvegarderTexte(struct composant *Sad);
void SauvegarderTexte(struct composant *Sad)
{
FILE *Fich;
int fermer=0;
/* Ouverture pour ecriture seule*/
if((Fich = fopen( "c:\\fichier.txt", "a" )) == NULL ) // "a" permet d'ajouter de nouveau element en fin de fichier
printf("-----------------Le Fichier na pas pu etre ouvert-----------------\n" );
else
{
printf("----------------------Le Fichier est ouvert-----------------------\n" );
/* Ecriture */
if(Sad!=NULL)
{
fprintf(Fich,"------------------\n");
fprintf(Fich,"Nom : %s\n",Sad->NOM);
fprintf(Fich,"Fabricant : %s\n",Sad->FABRICANT);
fprintf(Fich,"Fonction : %s\n",Sad->FONCTION);
fprintf(Fich,"Prix HT : %f Euros\n",Sad->PRIX);
fprintf(Fich,"------------------");
printf("\n SAUVEGARDE EFFECTUE \n" );
}
else
printf("\n !Aucun composant(s) present dans la liste chaine! \n");
/* Fermeture */
}
fermer = _fcloseall();
fclose(Fich);
if(fermer>0)
printf("\n--------------------------Fichier Fermer--------------------------\n");
else
printf("\n------------------------Fichier NON Fermer------------------------\n");
}
void AfficherTexte();
void AfficherTexte()
{
FILE *Fich;
char INPUT[255];
int fermer=0,i,c;
/* Ouverture pour lecture seule*/
if((Fich = fopen( "c:\\fichier.txt", "r" )) == NULL )
printf("-----------------Le Fichier na pas pu etre ouvert-----------------\n" );
else
{
printf("----------------------Le Fichier est ouvert-----------------------\n" );
/* Lecture */
if((c=fgetc(Fich)) != EOF)
{
printf("------------------------CONTENU DU FICHIER------------------------\n" );
while (!feof(Fich))// si on est pas arrivé a la fin
{
fgets(INPUT, 255, Fich); //On range la chaine dans INPUT
printf("%s",INPUT);
//fprintf(stdout,"%s",INPUT);
}
}
else
{
printf("\n !LE FICHIER EST VIDE! \n");
}
/* Fermeture */
fermer = _fcloseall();
fclose(Fich);
if(fermer>0)
printf("\n--------------------------Fichier Fermer--------------------------\n");
else
printf("\n------------------------Fichier NON Fermer------------------------\n");
}
}
int main(void)
{
struct composant * p;
int choix;
char Snom[20];
char Sfabricant[20];
char Sfonction[100];
float Sprix;
//CreerListeVide();
do{
printf("\n-----------------------------COMMANDES----------------------------\n");
printf("- 1 : Entrer un nouveau composant -\n");
printf("- 2 : Afficher le composant -\n");
printf("- 3 : Afficher tous les composants -\n");
printf("- 4 : Sauvegarder la liste de composant dans un fichier texte -\n");
printf("- 5 : Afficher tous les composants contenu dans le fichier texte -\n");
printf("- 6 : QUITTER -\n");
printf("------------------------------------------------------------------\n");
scanf("%d",&choix);
scanf("%*[^\n]"),getchar();
if(choix==1)
{
printf("\nSaisir Nom du composant\n");
gets(Snom);
printf("Saisir Fabricant du composant\n");
gets(Sfabricant);
printf("Saisir Fonction du composant\n");
gets(Sfonction);
printf("Saisir Prix du composant\n");
scanf("%f",&Sprix);
//if(p
InsertionEnAd(Snom,Sfabricant,Sfonction,Sprix);//,ter);
}
if(choix==2)
{
Afficher(ter);
}
/*if(choix==3)
{
AfficherTous(ter);
}*/
if(choix==4)
{
SauvegarderTexte(ter);
}
if(choix==5)
{
AfficherTexte();
}
}while(choix!=6);
} |
Partager