Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.02 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define tailleP 20
  5.  
  6. typedef struct
  7. {
  8.     char codeMat[6];
  9.     int coef;
  10.     char des[32];
  11. } Mat;
  12.  
  13. Mat lireMat(FILE *flot)
  14.  
  15. {
  16.     int l;
  17.     Mat a;
  18.     Mat *b;
  19.     b=&a;
  20.     fscanf(flot,"%s%d",b->codeMat, &b->coef);
  21.     fgets(b->des,32,flot);
  22.     l=strlen(b->des);
  23.     b->des[l-1]='\0';
  24.     return a;
  25. }
  26.  
  27. int chargeFmat(Mat tmat[],int nbmax)
  28.  
  29. {
  30.     int i=0;
  31.     Mat a;
  32.     Mat *b;
  33.     b=&a;
  34.     FILE *flot;
  35.     flot=fopen("tabMat.don","r");
  36.     if (flot==NULL)
  37.     {
  38.         printf("Erreur lors de l'ouverture du fichier.\n");
  39.         return -1;
  40.     }
  41.     tmat[i]=lireMat(flot);
  42.     while(!feof(flot))
  43.     {
  44.         if(i==nbmax)
  45.         {
  46.             printf("Dépassement de la capacité du tableau.\n");
  47.             return -2;
  48.         }
  49.         tmat[i]=lireMat(flot);
  50.         i=i+1;
  51.     }
  52.     fclose(flot);
  53.     return i;
  54. }
  55.  
  56.  
  57.  
  58. void test(void)
  59.  
  60. {
  61.     Mat tmat[20];
  62.     int i, j;
  63.     j=chargeFmat(tmat, tailleP);
  64.     for(i=0;i<j;i++)
  65.         printf("%s\t\t%d\t%s\n",tmat[i].codeMat, tmat[i].coef, tmat[i].des);
  66.     printf("%d\n",i);
  67. }
  68.  
  69. int main(void)
  70.  
  71. {
  72.     test();
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement