Guest User

Untitled

a guest
Feb 21st, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.39 KB | None | 0 0
  1. int FVAjoutFichier(const char* Info,FICHIERVAR Fich)
  2. {
  3.     ENREG   E;
  4.     int     Pos;
  5.     Trace("Entree dans FVAjoutFich() --%s--%d",Info,strlen(Info));
  6.     char*   pInter = Info;
  7.     //Pos = PremierLibre;
  8.     // verification si place ==> Remarque du prof, à faire !
  9.     int     i = 0, tailleTemp=1, tailleTotale=0, nbEnr;
  10.  
  11.     //*(Fich.Index +i) = PremierLibre;
  12.  
  13.     // boucle pour calculer le nombre d'emplacements pris puis comparer à la taille demandée pour l'enregistrement nouveau
  14.     for(i=0; i<Fich.Taille && tailleTemp; i++)
  15.     {
  16.     tailleTemp = *(Fich.Index+1+(2*i));
  17.     tailleTotale += tailleTemp;        
  18.     }    
  19.     i--; // Pour retomber à l'offset où placer dans l'index
  20.  
  21.     nbEnr = NB_ENR(strlen(Info), Fich.Taille);
  22.  
  23.  
  24.     // Pas assez de place, return 0
  25.     if(PremierLibre==0 || nbEnr > (Fich.Taille-tailleTotale-1)) return 0;
  26.  
  27.     Trace("NBENR_DEM : %2d ----- NBRESTE : %2d", nbEnr, Fich.Taille-tailleTotale-1);
  28.  
  29.     // Positionne à partir du premier emplacement libre
  30.     Pos = PremierLibre;
  31.  
  32.     // Enregistre dans l'index l'indice de début et le nombre d'emplacments pris
  33.     *(Fich.Index + i*2) = Pos;
  34.    // *(Fich.Index + Pos*2 + 1) = nbEnr;
  35.  
  36.     //i++;
  37.     while (strlen(pInter) > 20)
  38.     {
  39.     // Lecture de l'enregistrement (fichier => E) puis remplacement
  40.     // des caractères par ceux du nouveau message et, enfin, écriture dans le fichier
  41.     // ==> chainage gardé !
  42.         if (lseek(Fich.hdF,Pos * 24,SEEK_SET) == -1) return -1;
  43.         if (read(Fich.hdF,&E,24) != 24) return -1;
  44.         memcpy(E.Donnee,pInter,20);
  45.         if (lseek(Fich.hdF,Pos * 24,SEEK_SET) == -1) return -1;
  46.         if (write(Fich.hdF,&E,24) != 24) return -1;
  47.         Pos = E.Suivant;
  48.         pInter += 20;
  49.        // *(Fich.Index+i) = *(Fich.Index + i) + 1;
  50.     // Incrémente le nombre d'emplacements utilisés
  51.         *(Fich.Index+i*2+1) = *(Fich.Index+i*2+1) + 1;
  52.     }
  53.  
  54.  
  55.     // Pareil + Premier libre mis à jour et E.suivant indiquant fin de l'enregistrement complet
  56.     if (lseek(Fich.hdF,Pos * 24,SEEK_SET) == -1) return -1;
  57.     if (read(Fich.hdF,&E,24) != 24) return -1;
  58.     PremierLibre = E.Suivant;
  59.     memcpy(E.Donnee,pInter,20);
  60.     E.Suivant = 0;
  61.     if (lseek(Fich.hdF,Pos * 24,SEEK_SET) == -1) return -1;
  62.     if (write(Fich.hdF,&E,24) != 24) return -1;
  63.     //*(Fich.Index+i) = *(Fich.Index + i) + 1;
  64.     *(Fich.Index+i*2+1) = *(Fich.Index+i*2+1) +1;
  65.  
  66.     return 1;
  67. }
Add Comment
Please, Sign In to add comment