home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 17 / CD_ASCQ_17_101194.iso / dos / prg / alb_c10 / chap_15 / ch15_08.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-21  |  1.7 KB  |  62 lines

  1. /*********************************************************************
  2. *  CH15_08.C                         Fonctions fwrite() et fread() *
  3. *********************************************************************/
  4.  
  5. #include<stdio.h>
  6.  
  7. #define m 3
  8. #define dim 16
  9. #define fichier "c:\\albulus\\chap_15\\exercice.bin"
  10.  
  11. typedef struct     {   unsigned numero;
  12.             double mouvement,
  13.                       anc_solde,
  14.                       nve_solde;
  15.         } client;
  16.  
  17. main( void)
  18. {
  19.         int i;
  20.     FILE *flux;
  21.     client cl0[m]= { { 41111u, 1234.0, 0.0, 1234.0},
  22.              { 41112u, 5678.0, 0.0, 5678.0},
  23.              { 41113u, 9012.0, 0.0, 9012.0}
  24.                },
  25.            cl1[m], cl2;
  26.  
  27.     flux= fopen( fichier, "w+b");
  28.  
  29.   /* 1. Ecriture du tableau de structures dans le fichier.        */
  30.     if( fwrite( &cl0, sizeof( client), m, flux)!= m)
  31.       printf("\n Erreur d'Θcriture!\n");
  32.  
  33.   /* 2. Lecture du tableau de structures.                    */
  34.     fseek( flux, 0, 0);
  35.  
  36.     if( fread( &cl1, sizeof(cl1), 1, flux)!= 1 || feof( flux))
  37.       printf("\n Erreur de lecture n░1!\n");
  38.  
  39.   /* 3. Lecture d'un ΘlΘment du tableau.                */
  40.     fseek( flux, 0+ sizeof(client), 0);
  41.  
  42.     if( fread( &cl2, sizeof(client), 1, flux)!= 1)
  43.       printf("\n Erreur de lecture n░2!\n");
  44.  
  45.         fclose( flux);
  46.  
  47.   /* 4. Affichages.                            */
  48.     for( i= 0; i< m; i++)
  49.         {
  50.        printf("\n Le n░ du compte est %u , le solde %.2lf",
  51.                     cl1[i].numero, cl1[i].nve_solde);
  52.     }
  53.     printf("\n\n Le n░ du compte est %u , le solde %.2lf",
  54.                     cl2.numero, cl2.nve_solde);
  55. }
  56.  
  57. /*
  58. Le n░ du compte est 41111 , le solde 1234.00
  59. Le n░ du compte est 41112 , le solde 5678.00
  60. Le n░ du compte est 41113 , le solde 9012.00
  61.  
  62. Le n░ du compte est 41112 , le solde 5678.00                */