This repository has been archived on 2026-05-01. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
Projet-C-Huffman/gestion_des_fichiers/bit_a_bit.c
2019-12-07 20:39:59 +01:00

50 lines
No EOL
1.1 KiB
C

#include <stdio.h>
#include <assert.h>
#define BIN_MAX 8
void binaire(unsigned int n, char s[]);
int main(int argc, char **argv){
FILE *file;
char buffer;
int cursor,c,i;
file=fopen("text.txt","rb");
char text_b[5][BIN_MAX+1];
i=0;
// EOF : End Of File
while((c=fgetc(file))!=EOF){
char sb[BIN_MAX+1];
binaire(c,sb);
printf("%s\n",sb);
}
for(i=0;i<5;i++){
for(int j=0;j<BIN_MAX;j++){
printf("%c",text_b[i][j]);
}
}
fclose(file);
return 0;
}
void binaire(unsigned int n, char s[]){
/*
Décomposition binaire d'un entier (<BIN_MAX) et place celle-ci dans une chaîne de caractères passée en argument (s)
*/
assert(n>=BIN_MAX);
s[BIN_MAX]= '\0';
int i,r;
i=0;
while(n!=0){
r = n%2;
n = n/2;
if(0<=(BIN_MAX-i-1) && (BIN_MAX-i-1)<=BIN_MAX){
if(r==0) s[BIN_MAX-i-1]= '0';
else s[BIN_MAX-i-1]= '1';
i++;
}
}
int k;
for(k=BIN_MAX-i-1;k>=0;k--){
s[k]= '0';
}
}