diff --git a/gestion_des_fichiers/gestion_fichiers.c b/gestion_des_fichiers/gestion_fichiers.c index a96bfd3..bd20a99 100644 --- a/gestion_des_fichiers/gestion_fichiers.c +++ b/gestion_des_fichiers/gestion_fichiers.c @@ -1,4 +1,78 @@ /* Fonctions liés à la gestion de fichiers -*/ \ No newline at end of file +*/ +#include +#include +#include +#include "gestion_fichiers.h" + + +Bin_file *Ouv_Bit(char *p,char mode) +{ + Bin_file *A; + A=malloc(sizeof(Bin_file)); + A->mode=mode; + if(mode=='r') + { + A->file=fopen(p,"r"); + } + else + { + A->file=fopen(p,"w"); + } + A->record_length=0; + A->i_record=0; + A->i_octet=0; + A->nb_octets=0; + return A; +} + +void Ec_Bit(Bin_file *output,char bit) +{ + unsigned char octet,b; + int i; + + output->octet[output->i_octet]=bit; + output->i_octet++; + if (output->i_octet==8) + { + octet=0; + b=0x80; + for(i=0;i<8;i++) + { + if(output->octet[i]=='1') + { + octet=octet|b; + } + b=b>>1; + } + output->i_octet=0; + output->record[output->i_record]=octet; + printf("%c",output->record); + output->i_record++; + output->nb_octets++; + if(output->i_record==BLOCK_SIZE) + { + fwrite(output->record,BLOCK_SIZE,1,output->file); + output->i_record=0; + } + } + +} + +int main() +{ + Bin_file *p; + int i; + printf("test1\n"); + p=Ouv_Bit("test.txt",'w'); + printf("test2\n"); + for(i=0;i<8;i++) + { + Ec_Bit(p,'1'); + } + printf("test3\n"); + fclose(p); + return 0; +} \ No newline at end of file diff --git a/gestion_des_fichiers/gestion_fichiers.h b/gestion_des_fichiers/gestion_fichiers.h index c8aa5e3..52ec334 100644 --- a/gestion_des_fichiers/gestion_fichiers.h +++ b/gestion_des_fichiers/gestion_fichiers.h @@ -1,3 +1,18 @@ -/* - Prototypes de gestion_fichiers.c + variables define + structure(s) -*/ \ No newline at end of file +#ifndef __GESTION_FICHIERS__ +#define __GESTION_FICHIERS__ +#include +#define BLOCK_SIZE 4096 + +typedef struct +{ + FILE*file; // Identificateur fichier + char mode; //Mode de lecture r ou w + unsigned char record[BLOCK_SIZE]; //Tampon pour lire ou écrire + int record_length; //nombre d'élèments du tampon + int i_record; //indice dans le tampon + char octet[8]; //On découpe l'octet en 8 caractère + int i_octet; //indice dansl'octet + int nb_octets; //Nb octet lis/écrit +}Bin_file; + +#endif \ No newline at end of file